0
votes

I have a body of text like so:

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%.

I want to use PHP to scan through this body of text and look for any variable that has this pattern %variable_name% and then add that value to an array.

In the end I would have an array that looks like so:

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

How can this be accomplished?

3

3 Answers

3
votes

Thats basically exactly what preg_match_all is for:

$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

3
votes

You could use preg_match_all() :

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

Outputs :

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

This can be done without regex too.
You can use explode and explode on % and array items with spaces are not variables.

$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);

If you need to reset the array index you can use array_values to make it count from 0 again.

https://3v4l.org/909Zd


$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);