-2

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.

VAD
  • 13
  • 3
  • 3
    Possible duplicate of [PHP preg\_replace special characters](http://stackoverflow.com/questions/8483546/php-preg-replace-special-characters) – Don Apr 05 '17 at 20:20
  • 1
    Possible duplicate of [How to replace certain parts of my string?](http://stackoverflow.com/questions/8163746/how-to-replace-certain-parts-of-my-string) – Don't Panic Apr 05 '17 at 20:45
  • No able to find solution – VAD Apr 06 '17 at 13:25

2 Answers2

0

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

Derek
  • 2,927
  • 3
  • 20
  • 33
  • I am getting output as 3/26, 5/15, 5/20, 5/26-5/28. str_replace is not working in this case. – VAD Apr 06 '17 at 12:48
  • @VAD can you show the code? because I've tested and it outputs correctly [Link Here](http://www.tutorialspoint.com/execute_php_online.php?PID=0Bw_CjBb95KQMdmk4eVpYMkE1SlU) – Derek Apr 06 '17 at 14:53
0

$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.

ata
  • 3,398
  • 5
  • 20
  • 31
VAD
  • 13
  • 3