0
votes

Hi There I am hoping that someone can help me with this preg_replace problem that I have

I have the following preg_replace("/(\/[^\/]*.jpg)/", ".jpg", $input_lines);

the idea is to replace images in my wordpress site to their original name... The file already exists in the non cache folder so I do not need to worry about that part...

<img src="http://url.com/wp-content/uploads/cache/2014/02/flag/278439615.jpg">

I have a replace that takes care of the cache in the url so I am left with the URL like this:

<img src="http://url.com/wp-content/uploads/2014/02/flag/278439615.jpg">

Now the next step would be to strip the number.jpg from the URL the number can be alphabetical or numbers.

The constant in all URL's is the last occurance of the / and the .jpg which the preg_replace("/(\/[^\/]*.jpg)/", ".jpg", $input_lines); takes care of

The problem however is that the preg_replace needs to look for the tags in the image tag and not replace any code that is not in the image tag...

How would I add to the regex (preg_replace) to look only inside the image tags for the replacement?

The ideal end result would be a URL that looks like this:

<img src="http://url.com/wp-content/uploads/2014/02/flag.jpg">

The regex (preg_replace) also need to replace all occurances of the match (There might be more than one image on the page).

Your help would be much appreciated!

1

1 Answers

0
votes

You can use this replacement:

$pattern = <<<'EOD'
~
<img \s
(?> [^>s]++ | \Bs | s(?!rc\s*=) )* # possible content before the src attribute 
src \s* = \s* ["']? [^"\'\s>]+?    # start of the src attribute (until /cache/)
\K                                 # reset all from match result
/cache(?=/)
([^\s"'>]*?)                       # capture the path before the filename
/[^\s/]+ (?=\.jpg [>"'\s])         # the filename followed by the .jpg extension
~ix
EOD;
$result = preg_replace($pattern, '$1', $input_lines);