0

I was using c and c# for programming and I am using some third-party regular expression library to identify link pattern. But yesterday, for some reason, someone asked me to use php instead. I am not familiar with the php regular expression but I try, didn't get the result as expected. I have to extract and replace the link of an image src of the form :

<img src="/a/b/c/d/binary/capture.php?id=main:slave:demo.jpg"/>

I only want the path in the src but the quotation could be double or single, also the id could be vary form case to case (here it is main:slave:demo.jpg)

I try the following code

 $searchfor = '/src="(.*?)binary\/capture.php?id=(.+?)"/';
 $matches = array();
 while ( preg_match($searchfor, $stringtoreplace, $matches) == 1 ) {
   // here if mataches found, replace the source text and search again
   $stringtoreplace= str_replace($matches, 'whatever', $stringtoreplace);
 }

But it doesn't work, anything I miss or any mistake from above code?

More specifically, let say I have a image tag which give the src as

  <img src="ANY_THING/binary/capture.php?id=main:slave:demo.jpg"/>

here ANY_THING could be anything and "/binary/capture.php?id=" will be fixed for all cases, the string after "id=" is of pattern "main:slave:demo.jpg", the string before colon will be changed from case to case, the name of the jpeg will be varied too. I would expect to have it replaced as

  <img src="/main/slave/demo.jpg"/>

Since I only have right to modify the php script at specific and limit time, I want to debug my code before any modification made. Thanks.

user1285419
  • 2,183
  • 7
  • 48
  • 70

1 Answers1

0

First of all, as you may know, regex shouldn't be used to manipulate HTML.

However, try:

$stringtoreplace = '<img src="/a/b/c/d/binary/capture.php?id=main:slave:demo.jpg"/>';
$new_str = preg_replace_callback(
    // The regex to match
    '/<img(.*?)src="([^"]+)"(.*?)>/i',
    function($matches) { // callback
        parse_str(parse_url($matches[2], PHP_URL_QUERY), $queries); // convert query strings to array
        $matches[2] = '/'.str_replace(':', '/', $queries['id']); // replace the url
        return '<img'.$matches[1].'src="'.$matches[2].'"'.$matches[3].'>'; // return the replacement
    },
    $stringtoreplace // str to replace
);
var_dump($new_str);
Community
  • 1
  • 1
GManz
  • 1,548
  • 2
  • 21
  • 42