How to remove brackets from string in php?

58,140

Solution 1

Try with:

str_replace(array( '(', ')' ), '', $coords);

Solution 2

If brackets always come on beginging and end, you can use trim easily:

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

Result:

51.50972493425563, -0.1323877295303646

Solution 3

echo str_replace(
     array('(',')'), array('',''), 
     $coords);

or just do str_replace twice....

echo str_replace(')', '', str_replace('(','',$coords));

Solution 4

i think you need to write your coords here as a string else you get syntax error ;). Anyway, this is the solution i think.

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

$aReplace = array('(', ')');
$coordsReplaced = str_replace($aReplace , '', $coords);

Greets, Stefan

Share:
58,140
hairynuggets
Author by

hairynuggets

Web developer/entrepeneur. Codes php, likes codeigniter, css and jquery.

Updated on August 12, 2020

Comments

  • hairynuggets
    hairynuggets almost 4 years

    I have the following string and would like to use str_replace or preg_replace to remove the brackets but am unsure how. I have been able to remove the opening brackets using str_replace but can't remove the closing brackets.

    This is the sting:

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

    I have tried:

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

    which removed the opening brackets but am now under the impression that I need preg_replace to remove both.

    How does one go about this?

    Help appreciated