mikka23 Posted January 5, 2010 Share Posted January 5, 2010 Hi all. I have encountered a new problem which im not sure how to complete. Basically I have a string like: <li class="page_item page-item-21"><a href="http://localhost/wordpress/support/" title="Support">Support</a></li> I want a very simple function to only select the anchor from the rest of the string: <a href="http://localhost/wordpress/support/" title="Support">Support</a> Im not sure how to do this at all. Appreciate any help given. Quote Link to comment https://forums.phpfreaks.com/topic/187283-simple-function-to-select-only-a-part-of-a-string/ Share on other sites More sharing options...
premiso Posted January 5, 2010 Share Posted January 5, 2010 <?php $text = '<a href="http://www.ask.com" title="none">none</a>'; function returnAnchor($text) { preg_match('~href="(.*?)"~', $text, $matches); return (is_array($matches)?$matches[1]:''); } echo returnAnchor($text); ?> Using preg_match this is easily possible Quote Link to comment https://forums.phpfreaks.com/topic/187283-simple-function-to-select-only-a-part-of-a-string/#findComment-989008 Share on other sites More sharing options...
mikka23 Posted January 5, 2010 Author Share Posted January 5, 2010 Thanks for the reply, I was actually meaning select the whole link from the first string as opposed to taking the actual anchor tag. The actual inside of the link will change so it will have to be select everything from <a to </a>. im dont know how to do that however. Quote Link to comment https://forums.phpfreaks.com/topic/187283-simple-function-to-select-only-a-part-of-a-string/#findComment-989014 Share on other sites More sharing options...
premiso Posted January 5, 2010 Share Posted January 5, 2010 Is this more or less what you were after: $text = 'some string <a href="http://www.ask.com" title="none">none</a> exra string'; function returnAnchor($text) { preg_match('~<a(.*?)</a>~', $text, $matches); return (is_array($matches)?$matches[0]:''); } echo returnAnchor($text); Quote Link to comment https://forums.phpfreaks.com/topic/187283-simple-function-to-select-only-a-part-of-a-string/#findComment-989033 Share on other sites More sharing options...
mikka23 Posted January 5, 2010 Author Share Posted January 5, 2010 Yes, thanks so much. Thats exactly what I was looking for, now I have the following in my wordpress theme and it works perfectly <?php function returnAnchor($text) { preg_match('~<a(.*?)</a>~', $text, $matches); return (is_array($matches)?$matches[0]:''); } ?> <li class="grey"><?php echo returnAnchor(wp_list_pages('title_li=&include=21&echo=0')); ?> </li> Thats helped a lot, thanks! Quote Link to comment https://forums.phpfreaks.com/topic/187283-simple-function-to-select-only-a-part-of-a-string/#findComment-989040 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.