【问题标题】:PHP line break where lower-case letters meets uppercase-letter小写字母遇到大写字母的PHP换行符
【发布时间】:2014-03-18 05:02:22
【问题描述】:

我有一个字符串,如下所示:

$string = "New video gameSome TV showAnother item";

我希望能够单独访问每个项目,以获得类似于以下输出的内容:

New video game
Some TV show
Another item

如何在字符串中的每个项目名称或其他一些随机字符后添加一个 \n,以便以后分解成一个数组以单独访问字符串中的每个项目?

【问题讨论】:

  • 你最好让你的字符串项目用逗号或……分隔!
  • 谁来告诉TV 不是大写首字母,不应该拆分?
  • 是的,这就是字符串给我的方式......我正在尝试分隔其中的项目
  • 看起来很有挑战性,但这背后没有逻辑……是的,它在 TV 案例中失败了。
  • 你需要有一个模式才能知道你应该在哪里爆炸。如果大写字母是分隔符,所有其他字母都应该小写。

标签: php arrays explode


【解决方案1】:
$string = preg_replace('/([a-z])([A-Z])/', "\\1\n\\2", $string);

要回答您的评论以包含以右括号或数字结尾的单词:

$string = preg_replace('/([a-z0-9\)])([A-Z])/', "\\1\n\\2", $string);

【讨论】:

  • 这太棒了!!!您还可以解释可能以数字和结尾的单词吗?)? @Dave Meybohm
【解决方案2】:

我们(人类)可以根据物品对我们的意义来区分物品并识别它们。但是必须给计算机一个标准才能做到这一切。

您需要通过简单地添加分隔符(逗号或冒号)来更改存储字符串的方式,并且不要指望计算机会读懂我们的想法。

【讨论】:

    【解决方案3】:

    这个呢,

    $string = "New video game/nSome TV show/nAnother item";
    
    $string = explode("/n", $string);
    
    print_r( $string);
    

    【讨论】:

      【解决方案4】:

      这不是尝试分离项目的好方法。正如已经多次说过的那样,省去你的麻烦并使用分隔符,话虽如此,这会从给定的字符串返回预期的结果。

      $my_string = "New video gameSome TV showAnother item";
      preg_match_all('/[A-Z]{1}([A-Z]{2,5}|[a-z\s])+/',$my_string, $matches);
      var_dump($matches);
      

      但我敢肯定,如果您继续使用没有意义的模式,您会发现更多这种情况不起作用。

      [A-Z]{1} - find one uppercase letter
      ()+ - next pattern one or more times
      [A-Z]{2,5}|a-z\s - 2 -5  uppercase letters(for acronyms) OR lowercase letters and spaces
      

      这就是你在这里所要求的。祝你好运没有打破它。

      var dump 看起来像 - 没关系第二部分。

      array(2) { [0]=> array(3) { [0]=> string(14) "New video game" [1]=> string(12) "Some TV show" [2]=> string(12) "Another item" } [1]=> array(3) { [0]=> string(1) "e" [1]=> string(1) "w" [2]=> string(1) "m" } } 
      

      【讨论】:

        【解决方案5】:

        我做了一些比较来有效地查找大写字母,我只考虑第一个字符之后的大写字母,并且前面没有另一个大写字母或空格。

        <?php
        
        $s = "New video gameSome TV showAnother item";
        $i = 0;
        $j = 0;
        $phrases = array();
        $cap_bit = pow(2, 5);
        while($j < strlen($s))
        {
            $n = ord($s{$j});
        
            if(($n & $cap_bit) == 0 && 
               ($j == 0 || (
                    ord($s{$j - 1}) & $cap_bit) > 0 && 
                    $s{$j - 1} != ' ') && 
               $j > 0)
            {
                $phrases[] = substr($s, $i, $j - $i);
                $i = $j;
            }
        
            $j++;
        }
        
        
        $phrases[] = substr($s, $i);
        var_dump($phrases);
        

        结果:

        array(3) {
          [0]=>
          string(14) "New video game"
          [1]=>
          string(12) "Some TV show"
          [2]=>
          string(12) "Another item"
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2013-02-04
          • 2015-07-28
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-06-20
          • 2021-01-06
          • 2019-08-08
          相关资源
          最近更新 更多