How to remove brackets from string in php?

I have the following line and you want to use str_replace or preg_replace to remove the brackets, but I'm not sure how to do this. I was able to remove the opening brackets with str_replace, but I cannot remove the closing brackets.

This sting:

$coords = '(51.50972493425563, -0.1323877295303646)'; 

I tried:

 <?php echo str_replace('(','',$coords); ?> 

which removed the opening brackets, but now I feel like I need preg_replace to remove both.

How can I do that?

Help rate

+8
string php preg-replace str-replace
source share
5 answers

Try:

 str_replace(array( '(', ')' ), '', $coords); 
+37
source share

If the brackets always appear on the threshold and end, you can easily use trim :

 $coords = trim($coords, '()'); 

Result:

 51.50972493425563, -0.1323877295303646 
+28
source share

it's easier than you think str_replace can have an array as the first parameter

  <?php echo str_replace(array('(',')'),'',$coords); ?> 
0
source share
 echo str_replace( array('(',')'), array('',''), $coords); 

or just double str_replace ....

 echo str_replace(')', '', str_replace('(','',$coords)); 
0
source share

I think you need to write your coords here as a string, otherwise you will get a syntax error;). Anyway, this is the decision I think of.

 $coords = "(51.50972493425563, -0.1323877295303646)"; $aReplace = array('(', ')'); $coordsReplaced = str_replace($aReplace , '', $coords); 

Congratulations, Stefan

0
source share

All Articles