简体   繁体   中英

Removing special character from a string

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.

Use a regular expression:

$string = preg_replace('/(<a href.*profile.*>)@/U', '$1', $string);

Mind the ungreedy (U) modifier.

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

http://us3.php.net//manual/en/function.preg-replace.php http://us3.php.net/manual/en/reference.pcre.pattern.syntax.php

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM