Replace the character on sentence by white space

60 Views Asked by At

I have sentences string contains this character (X.V) X variable and i want replace this (X.V) by white space i do that :
preg_replace("/\([^)]+/","",$string); in function ($string) but not worked

original: $string=3 mail under (2.44) on 10 examples 1 CArd A2 (4.99)
ouput: $string= 3 mail under on 10 examples 1 card A 2

3

There are 3 best solutions below

2
On BEST ANSWER

Your expression preg_replace("/\([^)]+/","",$string); replaces opening parenthesis and all character that is not a closing parenthesis by nothing.

But you want to replace something like (X.V) by space, you could do:

preg_replace("/\([^)]+\)/", " ", $string);

In action:

$string = "3 mail under (2.44) on 10 examples 1 CArd A2 (4.99)";
$string = preg_replace("/\([^)]+\)/", " ", $string);
echo $string,"\n";

output:

3 mail under   on 10 examples 1 CArd A2
0
On

if you want to replace a particular character then, try

str_replace

str_replace("(X.V)"," ",$string);
0
On

if x and v are character and you want replace it by white space then you could use str_replace() function.put X,V into array and

$arr = array(X,V);
$string = str_replace($arr , '',$string);