MinusMyThoughts Posted June 21, 2007 Share Posted June 21, 2007 Hey, all.I'm trying to figure out a method with which I can parse user data to find links or email addresses and dynamically create a link.My gut tells me I'm going to need regular expressions to find the www.{content}.com or {content}@{content}.com and change them, and that means I have some learning to do.I've never used regular expressions, and all the slashes confuse me. Anyone care to explain a method to me so I can learn?Thanks!-Jason Link to comment Share on other sites More sharing options...
boen_robot Posted June 21, 2007 Share Posted June 21, 2007 Yup. You'll need regular expressions. There are many regular expression tutorials. One I find pretty interesting is The 30 Minute Regex Tutorial. I don't have a link to the one from which I've learned, but the basics are the same anyway. Link to comment Share on other sites More sharing options...
zppblood Posted June 21, 2007 Share Posted June 21, 2007 Here is a cheatsheet for regular expressions.http://www.ilovejackdaniels.com/cheat-shee...ns-cheat-sheet/ Link to comment Share on other sites More sharing options...
MinusMyThoughts Posted June 21, 2007 Author Share Posted June 21, 2007 **EDIT**This was completely off-base. Please disregard this post. Sorry!-Jason Link to comment Share on other sites More sharing options...
MinusMyThoughts Posted June 21, 2007 Author Share Posted June 21, 2007 After testing and playing, I came up with this script: <?php $content = 'For more information, ' . 'email jason@ennuidesign.com and ' . 'ask for Tim from http://demo.ennuidesign.com.<br /><br />' . 'Also visit http://php.net to check my work. ' . 'http://www.w3schools.com'; $pattern[0] = '/(\w+)\@(\w+)\.([A-z]{2,4})/'; $pattern[1] = '/(http:\/\/|www\.|http:\/\/www\.)(\w+|\w+\.\w+)\.([A-z]{2,4})/'; $replacement[0] = '<a href=mailto:$1@$2.$3>$1@$2.$3</a>'; $replacement[1] = '<a href=http://$2.$3 target=_blank>$2.$3</a>'; $fixed = preg_replace($pattern,$replacement,$content); echo <<<________EOD <html> <head> <title>Regular Expressions</title> </head> <body> <p align=center style=font-weight:900;font-size:12pt> Original Text: {$email}<br /><br /> Processed Text: {$fixed} </p> </body></html>________EOD;?> When I run this, it outputs the following: <html> <head> <title>Regular Expressions</title> </head> <body> <p align=center style=font-weight:900;font-size:12pt> Original Text: For more information, email jason@ennuidesign.com and ask for Tim from http://demo.ennuidesign.com.<br /><br />Also visit http://php.net to check my work. http://www.w3schools.com<br /><br /> Processed Text: For more information, email <a href=mailto:jason@ennuidesign.com>jason@ennuidesign.com</a> and ask for Tim from <a href=http://demo.ennu target=_blank>demo.ennu</a>idesign.com.<br /><br />Also visit <a href=http://php.net target=_blank>php.net</a> to check my work. <a href=http://www.w3schools.com target=_blank>www.w3schools.com</a> </p> </body></html> I can't wrap my head around a method to find subdomains (i.e. demo.ennuidesign.com). Any suggestions?Thanks!-Jason Link to comment Share on other sites More sharing options...
MinusMyThoughts Posted June 21, 2007 Author Share Posted June 21, 2007 Can someone please help me refine my expression to include a subdomain (such as http://w3schools.invisionzone.com) properly?Right now it takes the subdomain and the first 4 letters of the domain. Help!-Jason Link to comment Share on other sites More sharing options...
MinusMyThoughts Posted June 25, 2007 Author Share Posted June 25, 2007 Hey, all! I found the solution to my problem, but I figured I should toss it out for scrutiny since it's my first use of regex. //******************************************************** // This function replaces links and text email addresses // with a hyperlinked email address. //******************************************************** function findLinks($content) { $pattern[0] = '/(\w+)\@(\w+)\.([A-z]{2,4})/'; $pattern[1] = '/(http:\/\/|www\.|http:\/\/www\.)(\w+)\.(\w+\.\w+|\w+|\w+)(\/\w+)*/'; $replacement[0] = '<a href=mailto:$1@$2.$3 class=articleAuthor>$1@$2.$3</a>'; $replacement[1] = '<a href=http://$2.$3$4 class=articleAuthor target=_blank>$2.$3$4</a>'; $fixed = preg_replace($pattern,$replacement,$content); return $fixed; } I know that this works, but I also feel pretty confident in saying this could be more efficient. I'm just not sure how yet.Any suggestions?-Jason Link to comment Share on other sites More sharing options...
boen_robot Posted June 25, 2007 Share Posted June 25, 2007 I'll say drop the $fixed variable. Slightly less memory and slightly less CPU usage, since a variable is not created and referenced. I'd also put quotes around those attributes. Btw, isn't ":" a metacharacter?Thus: function findLinks($content) { $pattern[0] = '/(\w+)\@(\w+)\.([A-z]{2,4})/'; $pattern[1] = '/(http\:\/\/|www\.|http\:\/\/www\.)(\w+)\.(\w+\.\w+|\w+|\w+)(\/\w+)*/'; $replacement[0] = '<a href="mailto:$1@$2.$3" class="articleAuthor">$1@$2.$3</a>'; $replacement[1] = '<a href="http://$2.$3$4" class="articleAuthor" target="_blank">$2.$3$4</a>'; return preg_replace($pattern,$replacement,$content); } Regular expressions are more efficient then you might think. Once the regular expression library is loaded, it's cheeta fast. At least from what I've had in my (not so rich) experience with it. That's why PHP's authors suggest using str_replace() for simpler replacements - there's a big overhead of loading the library. Not much more, none less. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.