I have a str like :
'Test code {B}{X} we are implementing prototype {T} ,
using combinations of {U}{A} and {L/W}{F/K}.
I need to replace each occurance of {*} with it's corresponding code, so my resulting string would be:
'Test code <img src="../B.jpg"><img src="../X.jpg">
we are implementing prototype <img src="../T.jpg">
,using combinations of <img src="../U.jpg">
<img src="../A.jpg"> and <img src="../LW.jpg">
<img src="../FK.jpg">.
I don't wish to use str_replace and type out all the combinations because there is literally thousands of them.
$combinations = array("{B}", "{X}", "{W}{X},"{X/W}","{A/L}".."); etc
So i'm using preg_match_all to find all occurrences with the string.
function findMatches($start, $end, $str){
$matches = array();
$regex = "/$start([\/a-zA-Z0-9_]*)$end/";
preg_match_all($regex, $str, $matches);
return $matches[1];
}
Which returns to me,
Array ( [0] => B [1] => X [2] => T [3] => U [4] => A [5] => L/W [6] => F/K )
The problem is I don't need the '/' between letters, which I suppose I could str_replace later.
My question is how can I preg_replace using the array of matches and return the fully modified string back instead of the array?
preg_replace_callback. Also, it's totally unclear what exactly you are doing withfindMatches./\{(.)\}/and replace it with $1 that is matched group at index 1 within(). It works I have tested at here. For e.gpreg_replace('/\\{(.)\\}/', '$1', input_string)and there are 5 matches./\{([^}].*?)\}/as well that matches more than one letters inside {}.