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\-\._\?\,\\'\/\\\\\+&%\$#\=~])*)/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!
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\-\._\?\,\\'\/\\\\\+&%\$#\=~])*)/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!