#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    3
    Rep Power
    0

    Preg_match_all, then remove duplicate when loop using php


    preg_match_all, then remove duplicate when loop using php

    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...

    Full coding example will be appreciate. Thanks.
    Last edited by richprich; January 21st, 2013 at 11:03 AM. Reason: edit
  2. #2
  3. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,074
    Rep Power
    9398
    The first option (keeping the first use and discarding the rest) is easiest. Keep an array listing the images you've replaced, and if the current image already exists in there then just replace in an empty string.

    Oh, and preg_replace_callback() will be easier than using preg_match_all() and the option to include the offsets (which you aren't using).
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    3
    Rep Power
    0
    Originally Posted by requinix
    The first option (keeping the first use and discarding the rest) is easiest. Keep an array listing the images you've replaced, and if the current image already exists in there then just replace in an empty string.

    Oh, and preg_replace_callback() will be easier than using preg_match_all() and the option to include the offsets (which you aren't using).
    Hi,

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

    Thanks,

    Rich
  6. #4
  7. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,074
    Rep Power
    9398
    I'll give you most of it.
    PHP Code:
    $new preg_replace_callback('/regex/', function($matches) {
        static 
    $found = array();
        if (isset(
    $found[/* image number */])) {
            return 
    ""// already been replaced
        
    } else {
            
    $found[/* image number */] = true;
            return 
    /* whatever to use as the replacement */;
        }
    }, 
    $old); 
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    3
    Rep Power
    0
    Originally Posted by requinix
    I'll give you most of it.
    PHP Code:
    $new preg_replace_callback('/regex/', function($matches) {
        static 
    $found = array();
        if (isset(
    $found[/* image number */])) {
            return 
    ""// already been replaced
        
    } else {
            
    $found[/* image number */] = true;
            return 
    /* whatever to use as the replacement */;
        }
    }, 
    $old); 
    Perfect! Thanks a lot!

IMN logo majestic logo threadwatch logo seochat tools logo