Reputation: 465
I want to remove the special character @
from the following string
$string="Test <a href='/group/profile/1'>@Test User</a> another test <a href='/group/profile/2'>@User Test</a>";
Expected output:
<a href='/group/profile/1'>Test User</a> another test <a href='/group/profile/2'>User Test</a>"
Here i need to check each anchor tags in the string and need to find only the anchor tags with the word profile in href and need to remove the @
from the link text.If there is any @
outside the anchor tag in the string it should not be removed, only the @
in the anchor tag need to be removed.
Upvotes: 1
Views: 98
Reputation: 1680
Try This:
//remove @ from string
$string=str_replace('@','',$string);
//encode string using base64_encode function of php
$string=base64_encode($string);
//decode string using base64_decode function of php
echo $string=base64_decode($string); //Expected output
Upvotes: 0
Reputation: 733
Try:
$string = <<<EOT
@Do not remove
Test <a href='/group/profile/1'>@Test User</a> another test <a href='/group/profile/2'>@User Test</a>
@metoo
EOT;
$string = preg_replace('/(\<a\s+.+?\>.*?)(\@)(.*?\<\/a\>)/','$1$3',$string);
var_dump($string);
This can be buggy on big strings though
https://www.php.net//manual/en/function.preg-replace.php https://www.php.net/manual/en/reference.pcre.pattern.syntax.php
Upvotes: 0
Reputation: 4715
Use a regular expression:
$string = preg_replace('/(<a href.*profile.*>)@/U', '$1', $string);
Mind the ungreedy (U) modifier.
Upvotes: 3