【问题标题】:PHP, In a string, how to put two words beside each other or more in parentheses?PHP,在一个字符串中,如何将两个单词放在一起或多个放在括号中?
【发布时间】:2012-08-16 07:28:53
【问题描述】:

在一个字符串中,如何将两个或多个带有大写字母的单词放在括号中。示例:

   $string = "My name is John Ed, from Canada";

输出如下:(My) name is (John Ed), from (Canada)

【问题讨论】:

  • 如何将连字符之间的单词放在一个括号中? 例如:The 47-year-old, digs out racy behind-the-scenes snaps from 2007 是这样的:The (47-year-old), digs out racy (behind-the-scenes) snaps from 2007

标签: php regex string uppercase


【解决方案1】:

第一个想法可能如下所示:

<?php
    $str = "My name is John Ed, from Canada";
    echo preg_replace("/([A-Z]\\w*)/", "($1)", $str); //(My) name is (John) (Ed), from (Canada)
?>

(约翰·埃德)的事情应该有点棘手......

【讨论】:

    【解决方案2】:

    这个呢:

    <?php
      $str = "My name is John Ed, from Canada and I Do Have Cookies.";
      echo preg_replace("/([A-Z]{1}\w*(\s+[A-Z]{1}\w*)*)/", "($1)", $str); //(My) name is (John Ed), from (Canada) and (I Do Have Cookies).
    ?>
    

    【讨论】:

    • 它工作正常 :) 如果我想忽略两个单词之间的特殊字符,例如:$string = "My name is John Ed, from Canada, Quebec"; 您的解决方案将输出:(My) name is (John) (Ed), from (Canada), (Quebec) 我希望它是这样的: (My) name is (John) (Ed), from (Canada, Quebec)
    • 那是因为 '\s+' 只允许单词之间有空格。您可以扩展这部分,例如'[,\s]+' 以便也包含逗号。
    • 酷,工作正常 :) 你的新代码是:echo preg_replace("/([A-Z]{1}\w*([,\s]+[A-Z]{1}\w*)*)/", "($1)", $sentences[$i]); 谢谢大家
    • 如何将中间有连字符的单词放在一个括号中?例如:The 47-year-old, digs out racy behind-the-scenes snaps from 2007 变成这样:The (47-year-old), digs out racy (behind-the-scenes) snaps from 2007
    • echo preg_replace("/([A-Z]{1}[\w\-]*([,\s]+[A-Z]{1}[\w\-]*)*)/", "($1)", $sentences[$i]); 应该这样做(未经测试)。
    【解决方案3】:
    <?php
      $str = "My name is John Ed, from Canada";
      echo preg_replace('/([A-Z]\w*(\s+[A-Z]\w*)*)/', "($1)", $str);
    ?>
    

    【讨论】:

      【解决方案4】:

      如果您想与 unicode 兼容,请使用以下内容:

      $str = 'My name is John Ed, from Canada, Quebec, Saint-Laurent. My friend is Françoise';
      echo preg_replace('/(\p{Lu}\pL*(?:[\s,-]+\p{Lu}\pL*)*)/', "($1)", $str);
      

      输出:

      (My) name is (John Ed), from (Canada, Quebec, Saint-Laurent). (My) friend is (Françoise)
      

      解释:

      (           : start capture group 1
        \p{Lu}    : one letter uppercase
        \pL*      : 0 or more letters
        (?:       : start non capture group
          [\s,-]+ : space, comma or dash one or more times
          \p{Lu}  : one letter, uppercase
          \pL*    : 0 or more letters
        )*        : 0 or more times non capture group
      )           : end of group 1
      

      查看更多关于unicode properties

      【讨论】:

        【解决方案5】:
        $str = "My name is John Ed, from Canada\n";
        echo preg_replace("/([A-Z]\\w+( [A-Z]\\w+)*)/", "($1)", $str); //(My) name is (John Ed), from (Canada)
        

        试试这个

        【讨论】:

          猜你喜欢
          • 2018-03-17
          • 2012-03-31
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多