PDA

Click to See Complete Forum and Search --> : Possibly a preg question, splitting...


da_silvy
Jun 20th, 2003, 09:29 AM
If i had some text with:


opening tag:
closing tag:


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 http://www.microsoft.com/me.gif and as you can see, i'm http://www.microsoft.com/happy.gif 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 :p

The Hobo
Jun 20th, 2003, 12:13 PM
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.

bekkel
Jun 24th, 2003, 12:25 PM
function replace_img($text) {

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

return $text ;
}

and

echo replace_img($text);

phpman
Jun 24th, 2003, 01:15 PM
this has been answered many times on here. try a search

few come to mind

http://www.vbforums.com/showthread.php?s=&threadid=193978
http://www.vbforums.com/showthread.php?s=&threadid=190756

The Hobo
Jun 24th, 2003, 02:24 PM
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. :p

phpman
Jun 24th, 2003, 04:29 PM
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

da_silvy
Jun 28th, 2003, 10:51 AM
I couldn't get bekkel's thing to work properly, this is what I did the other day:


$images = spliti("\", $user["signature"]);
foreach ($images as $image) { // loop through the array - if they have multiple images
if (stristr($image, ""))
{
$image = eregi_replace("\[/img\]", "", $image); // Strip out the [/img]

// Affect the image here
}
}



Basically it's a signature size checker

:)

phpman
Jun 29th, 2003, 01:35 AM
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. :rolleyes:

da_silvy
Jun 29th, 2003, 10:34 PM
if you'd just read what i wanted, you'd know that i wanted every image tag into consecutive array placeholders


$text = "hi http://www.wang.com/ yo :D http://www.yo.com/";
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/ )

phpman
Jun 30th, 2003, 08:29 AM
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.

da_silvy
Jun 30th, 2003, 11:24 AM
sorry if i seem a little short and terse

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


sorry :(

phpman
Jun 30th, 2003, 08:35 PM
this works


$text = "http://www.microsoft.com/me.gif";
if(preg_match('/\[img]([^\"]*)\[\/img\]/siU', $text, $match )){
echo "found something = ". $match[1];
}else{
echo "didn't find anything";
}