PHP regular expression logical error
Hi,
I need some help. I designed a regular expression that takes a
and turns it into an image
My code works but if I have more than one image link, it only displays the first image? I must be overlooking something!
Here's my code:
<?php
$message = ereg_replace( \"(<a href='((http://|https://|www.|ftp://){1}([[:alnum:]]|[[:punct:]])+\.gif)'>([[:alnum:]]|[[:punct:]]|[[:blank:]])+</a>)\", \"<img target=\\"_blank\\" src=\\"\\2\\">\", $message );
?>
Thanks for your help!
Andrew Schools
bja888 (not verified) posted this at 22:05 — 9th December 2005.
They have: 5,633 posts
Joined: Jan 1970
what is the string you tested it aginst?
codehungry posted this at 22:53 — 9th December 2005.
He has: 10 posts
Joined: Feb 2004
Hi bja888,
This is the string I'm parsing:
<?php
$message = \"<a href='http://us.i1.yimg.com/us.yimg.com/i/us/my/addtomyyahoo4.gif'>image</a> <a href='http://us.i1.yimg.com/us.yimg.com/i/us/my/addtomyyahoo4.gif'>image</a>\";
?>
Thanks,
Andrew Schools
bja888 (not verified) posted this at 23:08 — 9th December 2005.
They have: 5,633 posts
Joined: Jan 1970
I am not familior with php but Regular expressions have been something I have used in other languages. It might be that you only ran the ereg_replace() only onece.
A proublem with your Regual Expression string...
add "http://www." to the first section.
codehungry posted this at 23:27 — 9th December 2005.
He has: 10 posts
Joined: Feb 2004
Hi,
I did only run the ereg_replace() function once, however, it’s supposed to find every occurrence and replace it. For example, if I run the following script:
<?php
$message = \"I went to the store and got a gallon of milk and a loaf of bread.\";
$message = ereg_replace(\"(a[[:alpha:]]d)\",\"---\", $message);
?>
My output is the following:
I went to the store --- got a gallon of milk --- a loaf of bread.
Thanks,
Andrew Schools
bja888 (not verified) posted this at 23:35 — 9th December 2005.
They have: 5,633 posts
Joined: Jan 1970
Well, ill give you 2 choices.
1) wait for someone who knows php to answer the thread. (Eveyone else but me)
2) Show me the output of the string you parced and hope I can come up with the answer.
codehungry posted this at 00:23 — 10th December 2005.
He has: 10 posts
Joined: Feb 2004
Hi, I get the following output:
<img target="_blank" src="http://us.i1.yimg.com/us.yimg.com/i/us/my/addtomyyahoo4.gif">
Thanks,
Andrew Schools
codehungry posted this at 04:41 — 10th December 2005.
He has: 10 posts
Joined: Feb 2004
I've been using
(http://|https://|www.|ftp://)+
that will match everything I need. Looking at the previous code, I must have changed it in my attempt to fix the problem. Here's the new code:
<?php
$message = \"<a href='http://us.i1.yimg.com/us.yimg.com/i/us/my/addtomyyahoo4.gif'>image</a> <a href='http://us.i1.yimg.com/us.yimg.com/i/us/my/addtomyyahoo4.gif'>image</a>\";
$message = ereg_replace(\"(<a href='((http://|https://|www.|ftp://)+([[:alnum:]]|[[:punct:]])+\.gif)'>([[:alnum:]]|[[:punct:]]|[[:blank:]])+</a>)\",\"<img target=\\"_blank\\" src=\\"\\2\\">\", $message);
?>
Thanks,
Andrew Schools
CptAwesome posted this at 09:37 — 11th December 2005.
He has: 370 posts
Joined: Dec 2004
$message = "<a href='http://us.i1.yimg.com/us.yimg.com/i/us/my/addtomyyahoo4.gif'>image</a> <a
href='http://us.i1.yimg.com/us.yimg.com/i/us/my/addtomyyahoo4.gif'>image</a>";
$regex = "(<a)?([A-z0-9 -=\"\'])+(href=)+(\'|\")+([A-z0-9\:\/\-_\.]{1,255})+(\.gif)+(\'|\")+>+[[:alnum:][:punct:]]+</a>";
$message = ereg_replace($regex,"<img src=\"\\5\\6\"><br />\n\n",$message);
echo $message;
CptAwesome posted this at 09:41 — 11th December 2005.
He has: 370 posts
Joined: Dec 2004
actuallt one small change to that and an example
<?
$message = "
<a href='http://us.i1.yimg.com/us.yimg.com/i/us/my/addtomyyahoo4.gif'>image</a>
<a target=\"_blank\" href='http://us.i1.yimg.com/us.yimg.com/i/us/my/addtomyyahoo4.gif'>image</a>
<a href='http://us.i1.yimg.com/us.yimg.com/i/us/my/addtomyyahoo4.gif' target=\"_blank\">image</a>
<a href=\"http://us.i1.yimg.com/us.yimg.com/i/us/my/addtomyyahoo4.gif\" target=\"_blank\">image</a>
";
$regex = "(<a)?([A-z0-9 -=\"\'])+(href=)+(\'|\")+([A-z0-9\:\/\-_\.]{1,255})+(\.gif)+(\'|\")?([A-z0-9 -=\"\'])+>+[[:alnum:][:punct:]]+</a>";
$message = ereg_replace($regex,"<img src=\"\\5\\6\"><br />\n",$message);
echo $message;
?>
returns:
<img src="http://us.i1.yimg.com/us.yimg.com/i/us/my/addtomyyahoo4.gif"><br />
<img src="http://us.i1.yimg.com/us.yimg.com/i/us/my/addtomyyahoo4.gif"><br />
<img src="http://us.i1.yimg.com/us.yimg.com/i/us/my/addtomyyahoo4.gif"><br />
<img src="http://us.i1.yimg.com/us.yimg.com/i/us/my/addtomyyahoo4.gif"><br />
codehungry posted this at 13:41 — 13th December 2005.
He has: 10 posts
Joined: Feb 2004
Thanks CptAwesome!
This is what I was looking for...
Thanks again,
Andrew Schools
Neutron2k posted this at 15:21 — 13th December 2005.
He has: 113 posts
Joined: Jul 2005
you will need to loop through the result set after executing the regexp.
Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.