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
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
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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.