简体   繁体   中英

How to replace special character ' /' in php with another special character

How to replace special character '/' in a string in php. I just want replace only this special character, not all special character

For eg,

$str= 3/26, 5/15, 5/20, 5/26-5/28;

I want to replace / with -.

I want to replace'/' with something as passing this string to form a xml is giving error.

You would use str_replace(search, replace, subject)

ex:

$str = '3/26, 5/15, 5/20, 5/26-5/28';
$str = str_replace("/", '-', $str);

outputs:

3-26, 5-15, 5-20, 5-26-5-28

$str = '3/26, 5/15, 5/20, 5/26-5/28'; was being pulled from Php-MyAdmin which is using html entities to get user input.

So when I decoded using function below:

html_entity_decode(str_replace('/', '/', $sValue), ENT_COMPAT | ENT_QUOTES | ENT_HTML5);

and then used str_replace , I was able to get the result.

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