fri3ndly Posted March 7, 2008 Share Posted March 7, 2008 Hi everyone I have (what I think is) a quick question... I have a variable called $article which contains a paragraph of text and a link at the bottom. How would I be able to do the following: Extract the <a href="link.html">Link Here</a> out of the variable and place it in to a new one? Quote Link to comment https://forums.phpfreaks.com/topic/94870-if-variable-contains-data-do-this/ Share on other sites More sharing options...
puritania Posted March 7, 2008 Share Posted March 7, 2008 You can extract it by using a Regular Expression with preg_match Quote Link to comment https://forums.phpfreaks.com/topic/94870-if-variable-contains-data-do-this/#findComment-485949 Share on other sites More sharing options...
fri3ndly Posted March 7, 2008 Author Share Posted March 7, 2008 Somethiing like this? preg_match("<a href=""></a>", $article) Im not really sure how to use this function properly and haven't found a useful guide yet Quote Link to comment https://forums.phpfreaks.com/topic/94870-if-variable-contains-data-do-this/#findComment-485951 Share on other sites More sharing options...
moon 111 Posted March 7, 2008 Share Posted March 7, 2008 I think preg_match("<a href=\"*\">*</a>", $article); would work. Quote Link to comment https://forums.phpfreaks.com/topic/94870-if-variable-contains-data-do-this/#findComment-485961 Share on other sites More sharing options...
Daniel0 Posted March 7, 2008 Share Posted March 7, 2008 preg_match('/<a href="(.*)">(.*)<\/a>/i', $article, $matches); The match will be stored in $matches. The href will be backreference 1, and the anchor text will be backreference 2. The above regex requires that the anchor tag is always formatted like that. Quote Link to comment https://forums.phpfreaks.com/topic/94870-if-variable-contains-data-do-this/#findComment-485963 Share on other sites More sharing options...
fri3ndly Posted March 7, 2008 Author Share Posted March 7, 2008 Thanks very much to both of you. Working perfectly :-) Quote Link to comment https://forums.phpfreaks.com/topic/94870-if-variable-contains-data-do-this/#findComment-485967 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.