PHP regular expression logical error

codehungry's picture

He has: 10 posts

Joined: Feb 2004

Hi,

I need some help. I designed a regular expression that takes a

This is a link to a image

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

They have: 5,633 posts

Joined: Jan 1970

what is the string you tested it aginst?

codehungry's picture

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

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's picture

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

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's picture

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's picture

He has: 10 posts

Joined: Feb 2004

Quote:
A proublem with your Regual Expression string...
add "http://www." to the first section.

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's picture

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's picture

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's picture

He has: 10 posts

Joined: Feb 2004

Thanks CptAwesome! Laugh

This is what I was looking for...

Thanks again,
Andrew Schools

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.