Home > database >  Replace multiple occurrence of a character in a string in php
Replace multiple occurrence of a character in a string in php

Time:10-04

$string = IT_Support_/_Help_Desk // what I have
$whatIwant = IT Support Help Desk // what I want

I have this string and I want to replace all accurrence of the space "_" to " "

CodePudding user response:

You can use preg_replace for this with a combination of non word characters \W and _ like below:

<?php

$string = "IT_Support_/_Help_Desk";

echo preg_replace('/[_\W] /', ' ', $string);

CodePudding user response:

$string = "IT_Support_/_Help_Desk";
$string = preg_replace('~[\\\\\/:*?"<>|_]~', ' ', $string);

echo $string;
  • Related