preg_replace help

Reply

Join Date: Dec 2008
Posts: 7
Reputation: bthaxor is an unknown quantity at this point 
Solved Threads: 0
bthaxor bthaxor is offline Offline
Newbie Poster

preg_replace help

 
0
  #1
Dec 7th, 2008
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:

  1. $theLink=__("link",wordspew); $theMail=__("email",wordspew);
  2.  
  3. $r->text = preg_replace("`(http|ftp)+(s)?:(//)((\w|\.|\-|_)+)(/)?(\S+)?`i", "<a href=\"\\0\" title=\"\\0\"$target>&raquo;&nbsp;$theLink&nbsp;&laquo;</a>", $r->text);
  4.  
  5. $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.
Last edited by bthaxor; Dec 7th, 2008 at 5:03 am.
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 1,402
Reputation: ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light 
Solved Threads: 224
Sponsor
ShawnCplus's Avatar
ShawnCplus ShawnCplus is offline Offline
Code Monkey

Re: preg_replace help

 
0
  #2
Dec 7th, 2008
((http://)?([\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.
  1. $link = preg_replace('#((http://)?([\w\-/\.]+)+\s)#', '\1', $r->text);
  2. $link = (strpos($link, 'http://') === false)) ? $link = 'http://'.$link : $link;
  3. $link = "<a href='$link' title='$link' rel='nofollow'>$link</a>';
  4.  
That gives you the fully prepared link.
Last edited by ShawnCplus; Dec 7th, 2008 at 2:08 pm.
GCS d- s+ a-->? C++(++++) UL+++ P+>+++ L+++ E--- W+++
N+ o K w++(---) O? !M- V PS+>++ PE+ Y+ PGP !t- 5? X- R tv+
b+>++ DI+ D G++>+++ e+ h+>++ r y+
PMs asking for help will not be answered, post on the forums. That's what they're there for.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 7
Reputation: bthaxor is an unknown quantity at this point 
Solved Threads: 0
bthaxor bthaxor is offline Offline
Newbie Poster

Re: preg_replace help

 
0
  #3
Dec 8th, 2008
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:

  1. $theLink=__("link",wordspew); $theMail=__("email",wordspew);
  2.  
  3. $r->text = preg_replace("`(http|ftp)+(s)?:(//)((\w|\.|\-|_)+)(/)?(\S+)?`i", "<a href=\"\\0\" title=\"\\0\"$target>&raquo;&nbsp;$theLink&nbsp;&laquo;</a>", $r->text);
  4.  
  5. $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
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC