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. Link to comment https://forums.phpfreaks.com/topic/81931-chopping-string-where-html-tags-are-also-embedded/ 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); ?> Link to comment https://forums.phpfreaks.com/topic/81931-chopping-string-where-html-tags-are-also-embedded/#findComment-416277 Share on other sites More sharing options...
mars_rahul Posted December 16, 2007 Author Share Posted December 16, 2007 that really didnt work. Link to comment https://forums.phpfreaks.com/topic/81931-chopping-string-where-html-tags-are-also-embedded/#findComment-416298 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. Link to comment https://forums.phpfreaks.com/topic/81931-chopping-string-where-html-tags-are-also-embedded/#findComment-416304 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.