...

View Full Version : repeating problem



abcdefGARY
02-25-2008, 12:19 AM
hello, I have a small question regarding PHP.

I have this snippet of code that I need to modify:


$preg = "/((?<!\")(http|https|ftp)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(:[a-zA-Z0-9]*)?\/?([a-zA-Z0-9\-\._\?\,\\'\/\\\\\+&amp;&#37;\$#\=~])*)/i";
if (preg_match_all($preg, $comment, $matches)) {
foreach ($matches[0] as $match) {
$match_str = str_replace(".", "\.", $match);
if (preg_match('/jpg|png|jpeg/i', substr($match, -5))) {
$comment = preg_replace("!".$match_str."!", '<img src="\0">', $comment);
} else {
$comment = preg_replace("!".$match_str."!", '<a rel="nofollow" target="_blank" href="\0">\0</a>', $comment);
}
}what it does is that it is being used for a comment textarea box. so that whenever a user inputs a URL or image URL like http://www.google.com or http://www.google.com/image.jpg, it will automatically be parsed into an url or image.

one single URL or image parses correctly. so if I put the following into the textarea:


http://www.google.com

it parses into:


<a rel="nofollow" target="_blank" href="http://www.google.com">http://www.google.com</a>

or when I place a single image URL into the textarea:


http://www.google.com/image.jpg

it parses into:


<img src="http://www.google.com/image.jpg">

my problem, however, is that if there are multiple hyperlinks or image URL's in the textarea, like this:


http://www.google.com
http://www.google.com

it parses into:


<a rel="nofollow" target="_blank" href="<a rel="nofollow" target="_blank" href="http://www.google.com">http://www.google.com</a>"><a rel="nofollow" target="_blank" href="http://www.google.com">http://www.google.com</a></a><br />
<a rel="nofollow" target="_blank" href="<a rel="nofollow" target="_blank" href="http://www.google.com">http://www.google.com</a>"><a rel="nofollow" target="_blank" href="http://www.google.com">http://www.google.com</a></a>

instead, it should be:


<a rel="nofollow" target="_blank" href="http://www.google.com">http://www.google.com</a><br />
<a rel="nofollow" target="_blank" href="http://www.google.com">http://www.google.com</a>

and if there are multiple image URLs, like this:


http://www.google.com/image.jpg
http://www.google.com/image.jpg

it would be parsed into something like:


<img src="<img src="http://www.google.com/image.jpg">"><br />
<img src="<img src="http://www.google.com/image.jpg">">

the best way to explain this would be like "a URL within a URL" or "an image within an image".

any help is appreciated. I've tried to explain this as best as I could, but if there are any misunderstandings, please let me know.

Thanks!

Inigoesdr
02-25-2008, 06:13 AM
This example might help a little:

$subject = <<<END

http://www.google.com/

http://www.google.com/logo.jpg


END;

$replace = array(
"#(?<!\")(https?://[^ ]+\.jpg|png|jpeg|gif)#ie" => "'<img src=\"' . trim('\\1') . '\" />'",
"#(?<!\")(http://[^ ]+)#ie" => "'<a href=\"' . trim('\\1') . '\">' . trim('\\1') . '</a>'",
);

$subject = preg_replace(array_keys($replace), array_values($replace), $subject);
Outputs:

<a href="http://www.google.com/">http://www.google.com/</a>

<img src="http://www.google.com/logo.jpg" />

abcdefGARY
02-26-2008, 07:04 AM
This example might help a little:

$subject = <<<END

http://www.google.com/

http://www.google.com/logo.jpg


END;

$replace = array(
"#(?<!\")(https?://[^ ]+\.jpg|png|jpeg|gif)#ie" => "'<img src=\"' . trim('\\1') . '\" />'",
"#(?<!\")(http://[^ ]+)#ie" => "'<a href=\"' . trim('\\1') . '\">' . trim('\\1') . '</a>'",
);

$subject = preg_replace(array_keys($replace), array_values($replace), $subject);
Outputs:

<a href="http://www.google.com/">http://www.google.com/</a>

<img src="http://www.google.com/logo.jpg" />
ahh, I am unfamiliar with PHP... I have no idea how to fix it based on the example you showed me, but does this work with multiple IMG and URL's for $subject?

I just need to stop the repeating in the code below... but I have no idea how.


$preg = "/((?<!\")(http|https|ftp)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(:[a-zA-Z0-9]*)?\/?([a-zA-Z0-9\-\._\?\,\\'\/\\\\\+&amp;&#37;\$#\=~])*)/i";
if (preg_match_all($preg, $comment, $matches)) {
foreach ($matches[0] as $match) {
$match_str = str_replace(".", "\.", $match);
if (preg_match('/jpg|png|jpeg/i', substr($match, -5))) {
$comment = preg_replace("!".$match_str."!", '<img src="\0">', $comment);
} else {
$comment = preg_replace("!".$match_str."!", '<a rel="nofollow" target="_blank" href="\0">\0</a>', $comment);
}
}

rafiki
02-26-2008, 09:21 AM
nobody here will do all the work for you, if you need it done asap hire someone. other wise take some time to learn.

oesxyl
02-26-2008, 02:34 PM
sorry for delay, try this:



<?php

$comment = "http://www.google.com/ comment after
http://www.google.com/

https://gmail.com/mail
https://yahoo.com/image

http://www.google.com/image.jpg
http://www.google.com/image.jpg";

$test = preg_replace("/\b(https|http|ftp)(\S+)/",
"<b style=\"color: #00f;\">$1$2</b>",
$comment);
$test = preg_replace("/\[url\](.+)\.(jpg|jpeg|png|gif)\[\/url\]/","$1.$2",
$test);
$regf = array("/\[url\]([^\[]+)\[\/url\]/",
"/\[img\]([^\[]+)\[\/img\]/");
$regr = array("<a rel=\"nofollow\" target=\"_blank\" href=\"$1\">$1</a>",
"<img src=\"$1\">");
$test = preg_replace($regf,$regr,$test);

$test = preg_replace("(\r?\n)","<br/>",$test);
print $test;
?>


is tested with what is in $comment variable, but you must do more test to be sure.

best regards

abcdefGARY
02-27-2008, 04:30 AM
hah! I managed to figure out how to stop the repeating problem by playing around and deleting one character after the other and see which one works.

the problem was in the bolded line:


$preg = "/((?<!\")(http|https|ftp)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(:[a-zA-Z0-9]*)?\/?([a-zA-Z0-9\-\._\?\,\\'\/\\\\\+&amp;&#37;\$#\=~])*)/i";
if (preg_match_all($preg, $comment, $matches)) {
foreach ($matches[0] as $match) {
$match_str = str_replace(".", "\.", $match);
if (preg_match('/jpg|png|jpeg/i', substr($match, -5))) {
$comment = preg_replace("!".$match_str."!", '<img src="\0">', $comment);
} else {
$comment = preg_replace("!".$match_str."!", '<a rel="nofollow" target="_blank" href="\0">\0</a>', $comment);
}
}

this:


$match_str = str_replace(".", "\.", $match);

should be:


$match_str = str_replace(".", ".", $match);

but thanks for your suggestions. :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum