【问题标题】:How to remove brackets in their content from a string in php [duplicate]如何从php中的字符串中删除其内容中的括号[重复]
【发布时间】:2016-08-05 15:46:10
【问题描述】:

我得到如下字符串:

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

我需要删除括号及其内部内容,以便只保留用逗号分隔的城市名称。

我只能找到查找和替换特定字符的解决方案,但没有任何方法可以解决问题。

【问题讨论】:

    标签: php


    【解决方案1】:

    在这里,我使用strpos 函数找到左括号并获取位置,然后在字符串中找到右括号位置。然后通过让开始和结束 I 减去结束位置来开始 + 1 。 最后通过使用substr_replace 函数,我从括号开始到结束的位置替换字符串中的任何字符。

    $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;
    

    【讨论】:

      猜你喜欢
      • 2017-05-06
      • 2013-08-12
      • 1970-01-01
      • 2012-02-16
      • 1970-01-01
      • 1970-01-01
      • 2019-10-12
      • 2021-12-31
      • 1970-01-01
      相关资源
      最近更新 更多