这也可以在没有正则表达式的情况下完成。
您可以在 % 上使用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);