【问题标题】:How to Split words using php?如何使用php拆分单词?
【发布时间】:2016-02-10 10:17:10
【问题描述】:

我想拆分一个小字。我的话写在下面。

{你:真棒;感觉不错}

我想通过使用 php 来拆分上面的单词来获得 feeling good 这个词

【问题讨论】:

  • explode(";","you: awesome; feel good");
  • 它的完整字符串? {你太棒了;感觉不错}

标签: php


【解决方案1】:
$arr = explode(';', trim("{you: awesome; feeling good}", '{}'));
$feel_good_string = trim($arr[1]);
echo $feel_good_string;

【讨论】:

  • 这是一个很好的方法,我从来没有在 {} 之前使用它。干得好兄弟。
【解决方案2】:

其他选择是......

$str = "{you: awesome; feeling good}";
$str = trim($str,"{}");
echo substr($str,strpos($str,";")+1);

【讨论】:

    【解决方案3】:

    您可以在 PHP 中使用explode() 来分割字符串。

    示例 1:

    $string = '{you: awesome; feeling good}'; // your string
    preg_match('/{(.*?)}/', $string, $match); // match inside the {}
    $exploded = explode(";",$match[1]); // explode with ;
    echo $exploded[1]; // feeling good
    

    示例 2:

    $string = '{you: awesome; feeling good}'; // your string
    $exploded = explode(";", $string);  // explode with ;
    echo rtrim($exploded[1],"}"); // rtrim to remove ending } 
    

    【讨论】:

      猜你喜欢
      • 2011-05-30
      • 2013-06-18
      • 1970-01-01
      • 1970-01-01
      • 2017-01-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多