Member Avatar for bthaxor

hey all:

i am using the wordspew shoutbox plugin for wordpress (at http://bthaxor.com), and have it customised exactly the way i want it except for one little problem.

inbuilt is a function which converts text beginning with http, ftp, https etc. to links in the shoutbox. i have one problem: it does not convert links beginning with 'www.', where the user has forgotten to place a 'http://', because they are recognised as relative links.

here are the three lines of code that do the replacing in wordspew:

$theLink=__("link",wordspew); $theMail=__("email",wordspew);

$r->text = preg_replace("`(http|ftp)+(s)?:(//)((\w|\.|\-|_)+)(/)?(\S+)?`i", "<a href=\"\\0\" title=\"\\0\"$target>&raquo;&nbsp;$theLink&nbsp;&laquo;</a>", $r->text);

$r->text = preg_replace("`([-_a-z0-9]+(\.[-_a-z0-9]+)*@[-a-z0-9]+(\.[-a-z0-9]+)*\.[a-z]{2,6})`i","<a href=\"mailto:\\1\" title=\"mailto:\\1\">&raquo;&nbsp;$theMail&nbsp;&laquo;</a>", $r->text);

i do not know php: can someone please incorportate 'www.' conversion as well?

thanks.

(([url]http://)?([/url][\w\-/\.]+)+\s) is a much simpler regular expression and does the same thing. Match 1 is your link so you can pretty much just replace your regex with that and replace \\0 with \\1.
However it can't really be fixed by a one-liner.

$link = preg_replace('#((http://)?([\w\-/\.]+)+\s)#', '\1', $r->text);
$link = (strpos($link, 'http://') === false)) ? $link = 'http://'.$link : $link;
$link = "<a href='$link' title='$link' rel='nofollow'>$link</a>';

That gives you the fully prepared link.

Member Avatar for bthaxor

hey shawn,

thanks for your help.

the code seems logical, however when i implement it it doesnt work (i get "Parse error: syntax error, unexpected ')' in /home/bthaxor/public_html/wp-content/plugins/wordspew/wordspew.php on line 748"). remember, i do not know php...

i believe you have not read the first line, here is the original code again:

$theLink=__("link",wordspew); $theMail=__("email",wordspew);

$r->text = preg_replace("`(http|ftp)+(s)?:(//)((\w|\.|\-|_)+)(/)?(\S+)?`i", "<a href=\"\\0\" title=\"\\0\"$target>&raquo;&nbsp;$theLink&nbsp;&laquo;</a>", $r->text);

$r->text = preg_replace("`([-_a-z0-9]+(\.[-_a-z0-9]+)*@[-a-z0-9]+(\.[-a-z0-9]+)*\.[a-z]{2,6})`i","<a href=\"mailto:\\1\" title=\"mailto:\\1\">&raquo;&nbsp;$theMail&nbsp;&laquo;</a>", $r->text);

the mailto can be ignored as it is fine...

thanks again

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.