0

Here are the ingredients:

  • html text
  • oldpath, e.g. /assets/uploads/
  • newpath, e.g. /assets/newFolder/
  • oldfilename, e.g. oldImage.jpg
  • newfilename, e.g. newImage.jpg

in the html there are images like this:

<img src="[oldpath]xxx[oldfilename]" />

What I want to do is write a regex that can replace instances of this with a new file name and path using PHP and/or MYSQL (prefer PHP) so that it becomes:

 <img src="[newpath]xxx[newfilename]" />

xxx are a bunch of characters, e.g. /_resampled/croppedimage170170-, but I dont know the exact content of xxx.

So i want to end up with something like

<?php
$newHtml = preg_replace(MAGIC GOES HERE);

Thank you in advance.

Nicolaas

1 Answers1

1

The regex should be /oldPath(.*?)old filename/ and the replacement would be newPath$1newFilename. I.e.

preg_replace('/oldpath(.*?)oldfilename/', 'newpath$1newfilename', originalHTMLstring);
keyneom
  • 815
  • 10
  • 12