PHP返回与模式匹配的文本正文中的所有变量的列表

问题描述 投票:0回答:3

我有一个像这样的文本体:

Lorem Ipsum%final_walk_through_date%只是打印和排版行业的虚拟文本。 %buyer_full_name%Lorem Ipsum自16世纪以来一直是业界标准的虚拟文本,当时一个未知的打印机拿了一个类型的厨房并拼写它来制作一个类型的样本书。它不仅存在了五个世纪,而且还延续了电子排版,基本保持不变。它在20世纪60年代随着包含Lorem Ipsum段落的Letraset表格的推出而普及,最近还推出了像Aldus PageMaker这样的桌面出版软件,其中包括Lorem Ipsum%purchase_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
3个回答
3
投票

你可以使用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%
)

2
投票

这基本上就是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


1
投票

这也可以在没有正则表达式的情况下完成。 您可以在%上使用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 version of it:
$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);
© www.soinside.com 2019 - 2024. All rights reserved.