Jump to content

Search for <a href="


johnsmith153

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/232446-search-for/#findComment-1195678
Share on other sites

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);

?>

Link to comment
https://forums.phpfreaks.com/topic/232446-search-for/#findComment-1195701
Share on other sites

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?

Link to comment
https://forums.phpfreaks.com/topic/232446-search-for/#findComment-1195734
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/232446-search-for/#findComment-1195752
Share on other sites

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/

Link to comment
https://forums.phpfreaks.com/topic/232446-search-for/#findComment-1195769
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.