
07-11-2010, 03:02 AM
|
|
Disabled
|
|
Join Date: Dec 2003
Posts: 1,941
|
|
preg_replace for url please?
Hello, after all of these years of trying I still cannot learn complex preg_replace expressions. Please do not bother pointing me to a tutorial since it may be just something my brain refuses to learn. I would like to have it detect urls and to add a <span class="blah">text</span> within the url. So when it detects:
Code:
<a href="url">text</a>
it replaces it with:
Code:
<a href="url"><span class="blah">text</span></a>
Could you please provide me with a working example?
Last edited by lexington; 07-11-2010 at 03:13 AM.
|

07-11-2010, 03:58 AM
|
|
Community Guide
|
|
Join Date: Jan 2006
Location: Athens, Greece
Posts: 1,479
|
|
PHP Code:
$str = '<a class="b" href="http://www.test.com">This is a test</a>'; echo preg_replace('/(<a\b[^>]*>)(.*?)(<\/a>)/i', '$1<span class="blah">$2</span>$3', $str);
Why not though, add a class to the a tag and not add the span tag?
|

07-11-2010, 04:40 AM
|
|
Disabled
|
|
Join Date: Dec 2003
Posts: 1,941
|
|
Thanks but it doesn't seem to replace the a href code. There are no errors, it is as though I never made any changes. I noticed you added a "class="b" within the href code, perhaps that is the reason it doesn't work?
Last edited by lexington; 07-11-2010 at 04:44 AM.
|

07-11-2010, 07:34 PM
|
|
Disabled
|
|
Join Date: Dec 2003
Posts: 1,941
|
|
|

07-11-2010, 11:36 PM
|
|
Web Hosting Master
|
|
Join Date: Oct 2002
Location: Vancouver, B.C.
Posts: 1,867
|
|
PHP Code:
$str = '<a href="url">text</a>'; $str = preg_replace('/<a href=[\'\"]((?:http:\/\/)?[a-zA-Z0-9:\._-]+)[\'\"]>(.*?)<\/a>/', "<a href=\"$1\"><span class=\"blah\">$2</span></a>", $str); print "$str\n";
__________________
Han Hwei Woo, ASTUTE HOSTING AS54527 *Advanced and customized solutions for the savvy customer!*
Dedicated Hosting and CDN out of Vancouver, Seattle, LA, Toronto, NY, Miami, and (soon) London
We include CDN, anycast DNS, onboard KVMoIP, firewall, local and global load-balancing, and privatenet with all servers.
sales@astutehosting.com
|

07-11-2010, 11:50 PM
|
|
Disabled
|
|
Join Date: Dec 2003
Posts: 1,941
|
|
Sorry that doesn't work either. You guys know that it is a huge paragraph with text and images, and it has to locate the hyperlink from all of the text and add the span tags to it?
|

07-12-2010, 12:17 AM
|
|
Disabled
|
|
Join Date: Dec 2003
Posts: 1,941
|
|
Actually nevermind, I actually found a pregmatch that the script used already in another included file and added my new span class there and it did the job. Thanks for taking the time to help me 
|

07-12-2010, 10:15 AM
|
|
Web Hosting Master
|
|
Join Date: May 2009
Posts: 766
|
|
Why are you using preg_match/replace? This is definitely a job for DOMDocument...
|

07-13-2010, 05:18 PM
|
|
Disabled
|
|
Join Date: Dec 2003
Posts: 1,941
|
|
Can someone change the value of a PayPal form?
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
| Postbit Selector |
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|
|
| Login: |
|
|
| Advertisement: |
|
|
| Web Hosting News: |
|
|
|