0

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?

3
  • 3
    You are using the wrong tool for the job. What you want is preg_replace_callback. Also, it's totally unclear what exactly you are doing with findMatches. Commented Jun 20, 2014 at 21:17
  • No idea about this language. but try /\{(.)\}/ and replace it with $1 that is matched group at index 1 within (). It works I have tested at here. For e.g preg_replace('/\\{(.)\\}/', '$1', input_string) and there are 5 matches. Commented Jun 20, 2014 at 21:20
  • 1
    Try /\{([^}].*?)\}/ as well that matches more than one letters inside {}. Commented Jun 20, 2014 at 21:28

2 Answers 2

3

I suggest using preg_replace_callback() to achieve this. You can then use the str_replace() method to replace the forward slash / in your match that the callback function returns.

$text = <<<DATA
Test code {B}{X} we are implementing prototype {T} ,
 using combinations of {U}{A} and {L/W}{F/K}. 
DATA;

$text = preg_replace_callback('~{([^}]*)}~', 
      function($m) {
         return '<img src="../' . str_replace('/', '', $m[1]) . '.jpg">';
      }, $text);

echo $text;

Working Demo

Sign up to request clarification or add additional context in comments.

1 Comment

ah this works great thanks didn't know about preg_replace_callback
1

This will get you halfway there, but you still need one more replace to remove the /

<?php
$input='Test code {B}{X} we are implementing prototype {T} ,
 using combinations of {U}{A} and {L/W}{F/K}.';

$output = preg_replace("/{([^}]*)}/", '<img src="../' . '\\1' . '.jpg">', $input);
echo $output."\n";
?>

Output:

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="../L/W.jpg"><img src="../F/K.jpg">.

1 Comment

how can i replace the '/' inside of {L/W}{F/K} to be just LW.jpg or FK.jpg?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.