johnsmith153 Posted April 1, 2011 Share Posted April 1, 2011 I want to search through some text and... (a) pull out the web address inside <a href="here" (b) replace the entire <a href="">xxx</a> with new text Any ideas? Quote Link to comment Share on other sites More sharing options...
dcro2 Posted April 1, 2011 Share Posted April 1, 2011 Something like this maybe: <?php $text = '<a href="http://theurl.com/">link</a>'; $replacement = 'the replacement here'; //put the url inside $url if we find a match if(preg_match('/<a href="(.*?)">/i', $text, $matches)) { $url = $matches[1]; } //replace the whole <a> tag with $replacement $text = preg_replace('/<a href=".*?">.*?<\/a>/i', $replacement, $text); ?> It will only replace the first occurrence in your text. Quote Link to comment Share on other sites More sharing options...
johnsmith153 Posted April 1, 2011 Author Share Posted April 1, 2011 Any idea how to replace all occurences? Quote Link to comment Share on other sites More sharing options...
dcro2 Posted April 1, 2011 Share Posted April 1, 2011 Err.. sorry, I got confused. What I meant to say is it will only get the url from the first <a>. It will replace all occurrences. You could get all the urls like this: <?php $text = '<a href="http://theurl.com/">link</a>\n<a href="http://theurl2.com/">link2</a>'; $replacement = 'the replacement here'; //put the urls inside $urls if we find a match if(preg_match_all('/<a href="(.*?)">/i', $text, $matches)) { //$matches[1] contains all matches to the first subpattern $urls = $matches[1] } //replace all <a> tags with $replacement $text = preg_replace('/<a href=".*?">.*?<\/a>/i', $replacement, $text); ?> Quote Link to comment Share on other sites More sharing options...
johnsmith153 Posted April 1, 2011 Author Share Posted April 1, 2011 Brilliant. Thanks for taking the time to help. Also, I want to convert plain text links into the HTML hyperlink equivelant. So, "www.test.com" becomes "<a href='www.test.com'>ww.test.com</a> I have managed to do it, I think, but it also replaces the address in hyperlinks already set. So, Go to www.test.com or click <a href='www.test.com'>HERE</a> If I try and change all web links in the above it will also change the <a href='www.test.com'>HERE</a> part, which I don't want it to. Any ideas? Quote Link to comment Share on other sites More sharing options...
dcro2 Posted April 1, 2011 Share Posted April 1, 2011 No problem, just remember that my example was pretty simple and will fail on a lot of links that aren't written the same way. There's some more advanced regular expressions here for example. As for what you're asking for, I would probably convert all <a> tags into plain urls and then use something like this to convert them all back along with the unlinked ones. Quote Link to comment Share on other sites More sharing options...
johnsmith153 Posted April 2, 2011 Author Share Posted April 2, 2011 Thanks. I think you second url is a duplicate of the first. Do you have one to check for www. and http:// ? Thanks again. Quote Link to comment Share on other sites More sharing options...
dcro2 Posted April 2, 2011 Share Posted April 2, 2011 My bad. Meant to post this as the second link: http://www.gidforums.com/t-1816.html. Good luck! Edit: sorry, that only matches links starting with http(s):// Here's something that might work with some modifications: $t = " ".preg_replace( "/(([[:alnum:]]+:\/\/)|www\.)([^[:space:]]*)". "([[:alnum:]#?\/&=])/i", "<a href=\"\\1\\3\\4\" target=\"_blank\">". "\\1\\3\\4</a>", $t); From here: http://snipplr.com/view/29556/php-parse-url-mailto-and-also-twitters-usernames-and-arguments/ 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.