| | |
preg_replace help
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: Dec 2008
Posts: 7
Reputation:
Solved Threads: 0
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:
i do not know php: can someone please incorportate 'www.' conversion as well?
thanks.
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:
PHP Syntax (Toggle Plain Text)
$theLink=__("link",wordspew); $theMail=__("email",wordspew); $r->text = preg_replace("`(http|ftp)+(s)?:(//)((\w|\.|\-|_)+)(/)?(\S+)?`i", "<a href=\"\\0\" title=\"\\0\"$target>» $theLink «</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\">» $theMail «</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.
((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.
php Syntax (Toggle Plain Text)
$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>';
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.
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.
•
•
Join Date: Dec 2008
Posts: 7
Reputation:
Solved Threads: 0
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:
the mailto can be ignored as it is fine...
thanks again
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:
PHP Syntax (Toggle Plain Text)
$theLink=__("link",wordspew); $theMail=__("email",wordspew); $r->text = preg_replace("`(http|ftp)+(s)?:(//)((\w|\.|\-|_)+)(/)?(\S+)?`i", "<a href=\"\\0\" title=\"\\0\"$target>» $theLink «</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\">» $theMail «</a>", $r->text);
the mailto can be ignored as it is fine...
thanks again
![]() |
Similar Threads
- Converting html to bb using preg_replace (PHP)
- Replacing 2 characters with with preg_replace (PHP)
- preg_replace (PHP)
- preg_replace regular expression problem with bbcode (PHP)
- XHTML Complient parser? (HTML and CSS)
- Scanning for links (PHP)
- Having trouble with links on PHP site! Any help would be great! (PHP)
Other Threads in the PHP Forum
- Previous Thread: Restricting access to list page from login
- Next Thread: Sms facility
| Thread Tools | Search this Thread |
# 5.2.10 alexa apache api array beginner binary broken cakephp checkbox class clean clients cms code cron curl database date directory display dissertation dynamic echo echo$_get[x]changingitintovariable... email encode error fairness file files folder form forms function functions google href htaccess html image images include indentedsubcategory insert ip javascript joomla legislation limit link local login mail memberships menu mlm multiple multipletables mysql mysqlquery newsletters oop open paypal pdf persist php problem provider query radio random recursion remote rss script search server sessions sms sockets source space spam sql syntax system table tutorial update upload url validator variable variables video web youtube






