I'm using a chat bot script, if a user name was test@test.com
the bot will reply @ <a href= mailto:test@test.com>test@test.com</a>
with a mailto link. I want the reply to be only test@test.com
without the link, I tried preg_replace
and str_replace
but I don't really know the exact code to use, I've tried the following but didnt work !
$name = preg_replace('/<a href="([^<]*)">([^<]*)<\/a>/', '', $name);
The whole code I'm using for replacements is this:
$name = str_replace (chr(0xc2).chr(0xa0), "_", $name);
$name = str_replace ("'", "", $name);
$name = str_replace (""", '"', $name);
$name = str_replace ("&", "&", $name);
$name = str_replace ("<", "", $name);
$name = str_replace (">", "", $name);
$name = str_replace ("&", "_", $name);
$name = str_replace ("*", "_", $name);
$name = preg_replace('/[^ \p{L}\p{N} \@ \_ \- \.\#\$\&\!]/u', '', $name);
$name = preg_replace('/<a href="([^<]*)">([^<]*)<\/a>/', '', $name);
Why do you want to replace it? Just use preg_match() with a regex similar to this:
<a href=[^>]+>([^<]*)</a>
so overall your code would look like this
<?php
$regex = '#<a href=[^>]+>([^<]*)</a>#';
$email = '<a href= mailto:test@test.com>test@test.com</a>';
preg_match($regex, $email, $matches);
var_dump($matches[1]);
/*
output:
string(13) "test@test.com"
*/
?>
The answer above makes a lot of assumptions when doing the preg_replace so it's going to fail lots unfortunately :( Here's why...
I'm not saying my solution is 100% secure but I've tested it in scenarios I'm aware of and I think it's an upgrade from the answer above!...
$email = preg_replace("/<a.+?href.+?>.+?<\/a>/is","",$email);
The 'i' modifier makes it insensitive The 's' modifier takes into account links that might be broken with newline breaks.
I'd always recommend populating a string with different links in different formats, different orders etc. That's always the best way to test things work. Assuming eveyone types links as My test is going to get you into lots of sticky situations :)
Good luck!