Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

preg_replace/auto format problem please

Status
Not open for further replies.

bitdaf

IS-IT--Management
Feb 23, 2003
37
US
Hi all,

I'm using a news program I found on Hotscripts called phpFree News - I'm not a php programmer and the person that has taken this script up is stumped as well...so I hope one you guys can help out. :)

The auto formatting for urls has a problem.

When you enter an url in the text field it is suppose to format it as "clickable"... but entering results in this:


We can't figure out how to remove the break tag from the formatted url.

Here is the bit of code:

==========================================

$contents = preg_replace("/(^|\s)( "\\1<a href=\" $contents);

===========================================

Here is the entire section.
=============================
// Function: BBCode Replacement
function ConvertText ($contents)
{
$contents = preg_replace("/\[b\](.*?)\[\/b\]/si", "<b>\\1</b>", $contents);
$contents = preg_replace("/\[i\](.*?)\[\/i\]/si", "<i>\\1</i>", $contents);
$contents = preg_replace("/\[u\](.*?)\[\/u\]/si", "<u>\\1</u>", $contents);
$contents = preg_replace("/\[p\](.*?)\[\/p\]/si", "<p>\\1</p>", $contents);
$contents = preg_replace("/\[code\](.*?)\[\/code\]/si", "<blockquote><pre>\\1</pre></blockquote>", $contents);
$contents = preg_replace("/\[quote\](.*?)\[\/quote\]/si", "<blockquote>\\1</blockquote>", $contents);
$contents = preg_replace("/(^|\s)(http:\/\/\S+)/si", "\\1<a href=\"\\2\">\\2</a>", $contents);
$contents = preg_replace("/(^|\s)( "\\1<a href=\" $contents);
$contents = preg_replace("/\[url\](http|https|ftp):)\/\/\S+?)\[\/url\]/si", "<a href=\"\\1\\2\" target=\"_blank\">\\1\\2</a>", $contents);
$contents = preg_replace("/\[url\](\S+?)\[\/url\]/si", "<a href=\" target=\"_blank\">\\1</a>", $contents);
$contents = preg_replace("/\(.*?)\[\/url\]/si", "<a href=\"\\1\\2\" target=\"_blank\">\\3</a>", $contents);
$contents = preg_replace("/\[url=(\S+?)\](\S+?)\[\/url\]/si", "<a href=\"[URL unfurl="true"]http://\\1\"
target=\"_blank\">\\2</a>", $contents);
$contents = preg_replace("/\[email\](\S+?@\S+?\\.\S+?)\[\/email\]/si", "<a href=\"mailto:\\1\">\\1</a>", $contents);
$contents = preg_replace("/\(.*?)\[\/email\]/si", "<a href=\"mailto:\\1\">\\2</a>", $contents);
$contents = preg_replace("/\(\S+?)\[\/img\]/si", "<img src=\"\\1\" border=0 alt=\"\\1\">", $contents);
return $contents;
}
===============================================

Should anyone know how to fix this we would be very appreciative! Please let me know if you need more info.

Thanks
Daf
 
My suspicion is that this only happens if you write out urls like this:
[ignore] [URL unfurl="true"]www.whatever.com
[/url]
while url's on one line are ok: www.whatever.com[/ignore]

The author probably has the nl2br() function somewhere in the script. All newline chars are made into <br/> tags. Hence, the one liner works, the rest doesn't.
 
Hi DRJ478,

Thanks for the reply. The deal is that the problem only occurs with links when we don't use the bbcode. In other words it's the auto format that is having this issue.

It's suppose to automatically parse any text starting with " as a link.

Not knowing much php I may have quoted the wrong bit of text in my original post?

Thanks
Daf
 
The regular expression is fine. The problem is that the nl2br() function is called in the code before the regular expressions are executed.
The solution is to find the nl2br() and move it after the expressions are called.
 
Thank you very much. I'm going to pass this on to the current developer and see what he can do. :)

Thanks
Daf
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top