eatfishy Posted October 4, 2009 Share Posted October 4, 2009 I was reading the regular expression tutorial on a few websites and I thought I understand it. When I try to code it out, I was not getting result that I needed. Below is a blog and I want to search for certain phrase: --------Start Blog----------- If you wanna learn how I create this website, you can start by reading some of the material in the below link. This is how I got started on creating website. [weblink]www.htmlcodetutorial.com[/weblink] I can show you what I know. --------End Blog------------- I basically want to search for any phase like [weblink]www.htmlcodetutorial.com[/weblink] so that I can replace it with something like this <a href="www.htmlcodetutorial.com">www.htmlcodetutorial.com</a>. I tried a few expression and it's not working. I'm concern with searching the phrase and I'll get to the replacing epression part later. $occurence=preg_match("|[weblink].[\/weblink]|", $blogcontent, $match); echo $occurence; echo $match[0]; www.htmlcodetutorial.com Quote Link to comment Share on other sites More sharing options...
Mchl Posted October 4, 2009 Share Posted October 4, 2009 What does dot stand for in regular expression? Quote Link to comment Share on other sites More sharing options...
eatfishy Posted October 4, 2009 Author Share Posted October 4, 2009 I thought . stands for wildcard. [weblink]www.target.com[/weblink] [weblink]www.google.com[/weblink] [weblink]www.yahoo.com[/weblink] I want to be able to use a reg expression to pick up any of the above phrases. Quote Link to comment Share on other sites More sharing options...
Mchl Posted October 4, 2009 Share Posted October 4, 2009 It does, but for single character only Quote Link to comment Share on other sites More sharing options...
eatfishy Posted October 4, 2009 Author Share Posted October 4, 2009 ah...is there a wild card for multi characters? Quote Link to comment Share on other sites More sharing options...
Mchl Posted October 4, 2009 Share Posted October 4, 2009 There is, but it wouldn't work for you. |[weblink][^[]*[\/weblink]| should do the trick Here's probably the best tutorial http://www.regular-expressions.info/tutorial.html Quote Link to comment Share on other sites More sharing options...
eatfishy Posted October 4, 2009 Author Share Posted October 4, 2009 Thanks, but it only returns weblink. Is there a way to return to the whole phrase? ex: [weblink]www.google.com[/weblink] Quote Link to comment Share on other sites More sharing options...
Mchl Posted October 4, 2009 Share Posted October 4, 2009 Duh... forum removed some slashes from the code... <?php $occurence=preg_match_all("|\[weblink]([^[]*)\[\/weblink]|", $blogcontent, $match); you should have full matches in $match[0] and urls only in $match[1] Quote Link to comment Share on other sites More sharing options...
eatfishy Posted October 5, 2009 Author Share Posted October 5, 2009 I tried the code and it didn't work. It was returning ARRAY as the value. I decided to take another route around this by creating a function with the help of string position, string length, string find and string replacement to get what I need. Thanks again for the help. Quote Link to comment Share on other sites More sharing options...
nrg_alpha Posted October 5, 2009 Share Posted October 5, 2009 Getting an array as a result is normal (with preg_match_all, the results will be stored into array index 0) like so: $blogcontent = <<<EOF --------Start Blog----------- If you wanna learn how I create this website, you can start by reading some of the material in the below link. This is how I got started on creating website. [weblink]www.htmlcodetutorial.com[/weblink] I can show you what I know. More text [weblink]www.htmlcodetutorial2.com[/weblink] followed by yet more text. --------End Blog------------- EOF; preg_match_all('#\[weblink]([^[]*)\[/weblink]#', $blogcontent, $matches); echo '<pre>'.print_r($matches[0], true); // this is the info you seek I suppose you could always use a preg_replace callback to do the replacements: Example: $blogcontent = <<<EOF --------Start Blog----------- If you wanna learn how I create this website, you can start by reading some of the material in the below link. This is how I got started on creating website. [weblink]www.htmlcodetutorial.com[/weblink] I can show you what I know. More text [weblink]www.htmlcodetutorial2.com[/weblink] followed by yet more text. --------End Blog------------- EOF; function replacement($input){ return $input[0] = "<a href=\"$input[1]\">$input[1]</a>"; } $blogcontent = preg_replace_callback('#\[weblink]([^[]*)\[/weblink]#', 'replacement', $blogcontent); echo $blogcontent; If the weblink tags can be upper or lowercase, you can always throw an 'i' modifier after the closing delimiter to match it in any case (upper or lower). Quote Link to comment Share on other sites More sharing options...
eatfishy Posted October 7, 2009 Author Share Posted October 7, 2009 I appreciate the help. I think I will use regular expression to validate email address for the register page. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.