mars_rahul Posted December 16, 2007 Share Posted December 16, 2007 i got problem explained as : - i fetch the data from database which is as : $string = "This is the sample string. <a href=#>click here</a>. Other <b>Rome</b>"; but i need output only the portion of the string only with <a> tag and content inside of <a> tag and nothing else. output must be as : - "<a href=#>click here</a>" will u please tell me how to solve this problem. i am new to php so please explain me the process. Quote Link to comment Share on other sites More sharing options...
Vizor Posted December 16, 2007 Share Posted December 16, 2007 Regular expressions, look up preg_match(). also with a hyperlink at the <a> tag it ends in </a> not [/url] that's just bbcode. You'll want something like <?php $pattern = '<a\ href=(.+?)</a>'; //this is the regular expression, probably doesn't work. $haystack = $string; //this is your database data. $results = preg_match($pattern, $haystack, $matches); //$matches is an array of matches found by preg_match. print_r($matches); ?> Quote Link to comment Share on other sites More sharing options...
mars_rahul Posted December 16, 2007 Author Share Posted December 16, 2007 that really didnt work. Quote Link to comment Share on other sites More sharing options...
Vizor Posted December 16, 2007 Share Posted December 16, 2007 Fine here it is, the whole thing done for you :/ <?php $string = "This is the sample string. <a href=#>click here</a>. Other Rome"; $pattern = '^\<a\ href=(.+?)\</a\>^'; $result = preg_match($pattern, $string, $matches); print_r($matches); ?> And yes that does work. 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.