0

Although there are many similar questions and answers here and i've tried my best to make it work but no luck. This is my code:

$content = "Blah...blah...[image=1], 
            blah...blah...blah...[image=2],
            blah...blah...blah...[image=1], no more...";

function get_image($content) 
{ 
  $stripper = $content;   
  preg_match_all("/\[image=(.+?)\]/smi",$stripper, $search);
  $total = count($search[0]);    
    for($i=0; $i < $total; $i++) 
    {    
      $image_id = $search[1][$i];
      if($image_id > 0)
      {
        $image = 'This is an image: <img src="images/'.$image_id.'.jpg" />';
      }
      $stripper = str_replace($search[0][$i], $image, $stripper); 
    } 
  return $stripper; 
}    

I want to remove the duplicate "[image=1]" and return:

Blah...blah...This is an image: <image>,
blah...blah...blah...This is an image: <image>,
blah...blah...blah..., no more...

or

Blah...blah...,
blah...blah...blah...This is an image: <image>,
blah...blah...blah...This is an image: <image>, no more...

2
Contributors
2
Replies
3
Views
4 Years
Discussion Span
Last Post by richprich
0

I'm not sure it would be the best way, but you could use preg_replace_callback and use a global array to store whether the id is duplicate or not.

0

Hi pritaeas,

Can you write the code for me? I have no idea how to do that even i've read the function on php.net.

Thanks,

Rich

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.