-2

I get strings like following:

New York (25,00 €), London (25,00 €), ....

I need the remove the brackets with their inner content, so that only city names separeted by commas remain.

I could only find solutions to find and replace particular characters, but nothing that can solve the issue.

WpDoe
  • 476
  • 1
  • 7
  • 22

1 Answers1

1

Here I am finding the open bracket and getting the position by using strpos function and then find the close bracket position in the string. then by having both start and ending I minus end position to start + 1 . At the end by using substr_replace function I replace nothing to characters in the string from position that bracket starts till the end of it.

$string = 'New York (25,00 €)';
$from = '(';
$to = ')';
$findposfrom = strpos($string, $from);
$findposto = strpos($string, $to);
$length = ($findposto - $findposfrom)+1;
$replace = '';
$stringreplace = substr_replace($string, $replace, $findposfrom, $length);

echo $findposfrom.'</br>';
echo $findposto.'</br>';
echo $length.'</br>';
echo $stringreplace;
Reza G.
  • 61
  • 9