hosted by liquidweb


Go Back   Web Hosting Talk : Web Hosting Main Forums : Programming Discussion : preg_replace for url please?
Reply

Programming Discussion Discussions related to web programming languages and other related issues. Topics may include configuration, optimization, practical usage and database connectivity.
Forum Jump

preg_replace for url please?

Reply Post New Thread In Programming Discussion Subscription
 
Send news tip View All Posts Thread Tools Search this Thread Display Modes
  #1  
Old 07-11-2010, 03:02 AM
lexington lexington is offline
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.
Reply With Quote


Sponsored Links
  #2  
Old 07-11-2010, 03:58 AM
Steve_Arm Steve_Arm is offline
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?

__________________


Reply With Quote
  #3  
Old 07-11-2010, 04:40 AM
lexington lexington is offline
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.
Reply With Quote
Sponsored Links
  #4  
Old 07-11-2010, 07:34 PM
lexington lexington is offline
Disabled
 
Join Date: Dec 2003
Posts: 1,941
Anyone please?

Reply With Quote
  #5  
Old 07-11-2010, 11:36 PM
hhw hhw is offline
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

Reply With Quote
  #6  
Old 07-11-2010, 11:50 PM
lexington lexington is offline
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?

Reply With Quote
  #7  
Old 07-12-2010, 12:17 AM
lexington lexington is offline
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

Reply With Quote
  #8  
Old 07-12-2010, 10:15 AM
mattle mattle is offline
Web Hosting Master
 
Join Date: May 2009
Posts: 766
Why are you using preg_match/replace? This is definitely a job for DOMDocument...

Reply With Quote
  #9  
Old 07-13-2010, 05:18 PM
lexington lexington is offline
Disabled
 
Join Date: Dec 2003
Posts: 1,941
Can someone change the value of a PayPal form?

oops nevermind

Reply With Quote
Reply

Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP: I ask for a preg_replace explanation SNOWmanx Programming Discussion 5 12-21-2006 12:02 AM
preg_replace error..Help..!! killapix Programming Discussion 2 08-20-2006 05:09 PM
Need to change <P> to nothing using preg_replace... crEA-tEch Programming Discussion 11 01-29-2004 09:43 AM
Can anyone help me with a preg_replace call? michaeln Programming Discussion 0 03-13-2003 01:33 PM
multiple eregi_replace / preg_replace : help jayantk Programming Discussion 2 03-06-2003 06:03 AM

Related posts from TheWhir.com
Title Type Date Posted
Web Host Hostway Replaces Legacy Webmail with Open-Xchange Web Hosting News 2013-02-01 11:17:40
VDI Provider Pano Logic Goes Out of Business, Leaves Customers with Questions Web Hosting News 2012-11-01 14:30:59
Web Host SoftLayer Names New Chief Financial Officer Web Hosting News 2012-02-06 11:28:34
The Surprising Insights of Cloudonomics, Joe Weinman of HP Web Hosting News 2011-09-26 19:26:03
IT Services Firm DataChambers Launches Cloud Hosting Infrastructure Web Hosting News 2011-07-25 16:55:06


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes
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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump
Login:
Log in with your username and password
Username:
Password:



Forgot Password?
Advertisement:
Web Hosting News:



 

X

Welcome to WebHostingTalk.com

Create your username to jump into the discussion!

WebHostingTalk.com is the largest, most influentual web hosting community on the Internet. Join us by filling in the form below.


(4 digit year)

Already a member?