【问题标题】:PHP return a list of all the variables in a body of text that match a patternPHP 返回文本正文中与模式匹配的所有变量的列表
【发布时间】:2018-08-08 20:53:40
【问题描述】:

我有这样一段文字:

Lorem Ipsum %final_walk_through_date% 只是 印刷和排版行业。 %buyer_full_name% Lorem Ipsum 有 自 1500 年代以来一直是行业的标准虚拟文本,当时 不知名的打印机拿了一个打字机,并把它打乱成一个打字机 标本书。它不仅经历了五个世纪,而且 跳入电子排版,基本保持不变。它 在 1960 年代随着 Letraset 床单的发布而普及 包含 Lorem Ipsum 段落,最近还有桌面 发布软件,如 Aldus PageMaker,包括 Lorem 版本 Ipsum %buying_side_commission_percent%。 %escrow_officer_first_name% 和 %ccrs_date%。

我想使用 PHP 扫描这段文本并查找具有这种模式 %variable_name% 的任何变量,然后将该值添加到数组中。

最后我会得到一个如下所示的数组:

array[
     '%final_walk_through_date%',
     '%buyer_full_name%',
     '%buying_side_commission_percent%',
     '%escrow_officer_first_name%',
     '%ccrs_date%'
]

如何做到这一点?

【问题讨论】:

    标签: php arrays


    【解决方案1】:

    这基本上正是preg_match_all 的用途:

    $s = "Lorem Ipsum %final_walk_through_date% is simply dummy text of the printing and typesetting industry. %buyer_full_name% Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum %buying_side_commission_percent%. %escrow_officer_first_name% and %ccrs_date%.";
    
    preg_match_all('/%[^%]*%/', $s, $matches);
    
    var_dump($matches);
    

    https://3v4l.org/Rvc5n

    【讨论】:

      【解决方案2】:

      你可以使用preg_match_all()

      preg_match_all('~%\w+%~', $text, $matches);
      print_r($matches[0]);
      

      输出:

      Array
      (
          [0] => %final_walk_through_date%
          [1] => %buyer_full_name%
          [2] => %buying_side_commission_percent%
          [3] => %escrow_officer_first_name%
          [4] => %ccrs_date%
      )
      

      【讨论】:

        【解决方案3】:

        这也可以在没有正则表达式的情况下完成。
        您可以在 % 上使用explode 和explode,而带有空格的数组项是不是 变量。

        $str = "Lorem Ipsum %final_walk_through_date% is simply dummy text of the printing and typesetting industry. %buyer_full_name% Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum %buying_side_commission_percent%. %escrow_officer_first_name% and %ccrs_date%.";
        $arr = explode("%", $str . " "); 
        // Adding space to end of string to make sure it removes last part if it's not a variable
        $newArr = array_filter($arr, function($v){
            If(strpos($v, " ") === false) return $v;
        });
        
        var_dump($newArr);
        

        如果您需要重置数组索引,您可以使用 array_values 使其重新从 0 开始计数。

        https://3v4l.org/909Zd


        它的 mickmackusa 版本:
        $str = "%start_of_input% Lorem Ipsum %final_walk_through_date% is simply dummy text of the printing and typesetting industry. %buyer_full_name% Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum %buying_side_commission_percent%. %escrow_officer_first_name% and %ccrs_date%.";
        $arr = explode("%", " " . $str . " ");
        $newArr = array_filter($arr, function($v){
            return strpos($v, " ")===false ? true : false;
        });
        
        var_dump($newArr);
        

        【讨论】:

        • 请删除ARRAY_FILTER_USE_BOTH$k。你没有使用它们。 array_filter() 只对returning true/false 真正感兴趣,并且无论有条件的结果如何,都应该提供return。您需要在explodeing 之前为$str 添加一个空格,以防第一个单词是%-wrapped。我推荐的更改在这里:3v4l.org/p2Mph
        • 我的返回方式不需要前置。试试看,你会看到。在您的方法中,您需要它,但在我的方法中不需要。但另一个是有效的积分,谢谢!
        • 如果可以在字符串的开头有一个 %-wrapped 子字符串,则前置是必要的。这就是我修改输入数据的原因——显示边缘情况。
        • 对不起,你是对的。我应该在开头用引号或其他标点符号设置它——% 之前的一个或多个字符后面不跟空格。需要覆盖的效果是在正则表达式中对单词边界字符的欺骗。这就是在$str 的前后添加空格的原因。 3v4l.org/TqNrl您的代码适用于一种边缘情况,但不适用另一种——尽管它们是相同类型的边缘情况。
        • @mickmackusa 您的代码适用于一种边缘情况,但不适用另一种情况 不确定我是否同意。一般来说,与普通字母相比,您使用标点符号或引号开始字符串的频率如何?这不是我的代码仅适用于某种情况的情况,您使它在一种特定情况下中断。您可以在所有代码中找到此类案例。如果我们在字符串中使用数学,所有可能的解决方案都会中断。 x=(100*50%)-(250*25%) 正则表达式和explode 中的这个字符串都会使其失败。 3v4l.org/GCBns 。解析时没有故障安全的代码。
        猜你喜欢
        • 1970-01-01
        • 2014-07-15
        • 1970-01-01
        • 2010-12-12
        • 2012-10-13
        • 2023-03-24
        • 2018-11-22
        • 2020-12-02
        • 2021-10-12
        相关资源
        最近更新 更多