Results 1 to 12 of 12

Thread: Possibly a preg question, splitting...

  1. #1

    Thread Starter
    Conquistador
    Join Date
    Dec 1999
    Location
    Australia
    Posts
    4,527

    Possibly a preg question, splitting...

    If i had some text with:

    Code:
    opening tag: [img] 
    closing tag: [/img]
    with text in between and other text surrounding it too, how can i get just the text between the img tags, in an array

    i.e. the following is the text:

    hello this is and as you can see, i'm ok?

    and it would return, in an array

    http://www.microsoft.com/me.gif
    http://www.microsoft.com/happy.gif

    Don't know about the quality of my explanation - it'll have to do

  2. #2
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Right off hand, I'm not sure. But before I try a few things, can I ask what you're trying to do? Ie, is the array the only option.

    If so, I'll try a few things.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  3. #3
    Lively Member
    Join Date
    Apr 2003
    Location
    Netherlands
    Posts
    96
    function replace_img($text) {

    $text = preg_replace("!\[img\](.*?)\[/img\]!Usi", "<img src=\\1>", $text);

    return $text ;
    }

    and

    echo replace_img($text);

  4. #4
    Frenzied Member
    Join Date
    Nov 1999
    Posts
    1,337
    this has been answered many times on here. try a search

    few come to mind

    http://www.vbforums.com/showthread.p...hreadid=193978
    http://www.vbforums.com/showthread.p...hreadid=190756

  5. #5
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    You guys are answer the wrong question...

    bekkel - Where is the array in your code? He wanted it put in an array.

    phpman - I didn't check your links, so maybe you are answering it correctly.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  6. #6
    Frenzied Member
    Join Date
    Nov 1999
    Posts
    1,337
    yeah one of those is the famous Prokhaled thread,

    jsut doing preg_match() will put it in an array, so bekkel was close just needs to change is replace

  7. #7

    Thread Starter
    Conquistador
    Join Date
    Dec 1999
    Location
    Australia
    Posts
    4,527
    I couldn't get bekkel's thing to work properly, this is what I did the other day:

    PHP Code:
            $images spliti("\[img\]"$user["signature"]);
            foreach (
    $images as $image) { // loop through the array - if they have multiple images
                
    if (stristr($image"[/img]"))
                {
                    
    $image eregi_replace("\[/img\]"""$image); // Strip out the [/img]
            
                // Affect the image here
                
    }
            } 
    Basically it's a signature size checker


  8. #8
    Frenzied Member
    Join Date
    Nov 1999
    Posts
    1,337
    you said you wanted it in an array??

    if(preg_match('/\[(img)\].*([:&()+]).*\[/U', $text, $match)){
    echo "found something = ". $match[2];
    }else{
    echo "didn't find anything";
    }


    that will put whatever is in between the [img] tags into $match[2]

    allyou had to do was read those threads I posted.

  9. #9

    Thread Starter
    Conquistador
    Join Date
    Dec 1999
    Location
    Australia
    Posts
    4,527
    if you'd just read what i wanted, you'd know that i wanted every image tag into consecutive array placeholders

    PHP Code:
    $text "hi [img]http://www.wang.com/[/img] yo :D [img]http://www.yo.com/[/img]";
    if(
    preg_match('/\[(img)\].*([:&()+]).*\[/U'$text$match)){
        
    print_r($match);
    }else{
        echo 
    "didn't find anything";

    gave me: Array ( [0] => [img]http://www.wang.com/[ [1] => img [2] => : )

    my example above gives me:
    Array ( [0] => http://www.wang.com/ [1] => http://www.yo.com/ )

  10. #10
    Frenzied Member
    Join Date
    Nov 1999
    Posts
    1,337
    well excuse me, just a little bit of changing and it would do what you wanted. I just grabbed that code from old code I had. didn't test it, but it will do what you wanted.

  11. #11

    Thread Starter
    Conquistador
    Join Date
    Dec 1999
    Location
    Australia
    Posts
    4,527
    sorry if i seem a little short and terse

    i've just finished exams so i'm a little stressed and tired :P


    sorry

  12. #12
    Frenzied Member
    Join Date
    Nov 1999
    Posts
    1,337
    this works

    Code:
    $text = "[img ]http://www.microsoft.com/me.gif[/img]";
    if(preg_match('/\[img]([^\"]*)\[\/img\]/siU', $text, $match )){
     echo "found something = ". $match[1];
    }else{
    echo "didn't find anything";
    }
    Last edited by phpman; Jun 30th, 2003 at 08:46 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width