My goal here is to, within a string, find a supposed array's keys and in that string, replace these keys with the matching keys from the array.
I have a small, useful function that finds all my strings between 2 delimiters (sandbox link: https://repl.it/repls/UnlinedDodgerblueAbstractions):
function findBetween( $string, $start, $end )
{
$start = preg_quote( $start, '/' );
$end = preg_quote( $end, '/' );
$format = '/(%s)(.*?)(%s)/';
$pattern = sprintf( $format, $start, $end );
preg_match_all( $pattern, $string, $matches );
$number_of_matches = is_string( $matches[2] ) ? 1 : count( $matches[2] );
if( $number_of_matches === 1 ) {
return $matches[2];
}
if( $number_of_matches < 2 || empty( $matches ) ) {
return False;
}
return $matches[2];
}
Example:
findBetween( 'This thing should output _$this_key$_ and also _$this_one$_ so that I can match it with an array!', '_$', '$_')
Should return an array with the values ['this_key', 'this_one'] as it does. Question is, how can I take these and replace them with an associative array's values?
Assume my array is this:
[
'this_key' => 'love',
'this_one' => 'more love'
];
My output should be this:
This thing should output love and also more love so that I can match it with an array!
How can I achieve this?