0

I have html string like this

<input alt="" src="/global/admin/plugins/fileman/Uploads/Images/Mail_1.jpg" style="width: 600px; height: 600px;" type="image" />

and want to make two changes it is using preg_replace

1- change tag to input == > img

2 -add the website link to the src path

I am trying to use this code and i's working good but the result has double quotations like below and this broke the link,i want to add the website URL without additional quotations

<img width="600" src="http://mywebsite.com/"/global/admin/plugins/fileman/Uploads/Images/Mail_1.jpg">

this is the php code

$pattern = "/(<input\s+).*?src=((\".*?\")|(\'.*?\')|([^\s]*)).*>/is";
$base = 'http://mywebsite.com/';
$replacement = "<img width=600 src=$base$2>";
echo preg_replace($pattern, $replacement, $html);
user1080247
  • 1,076
  • 4
  • 21
  • 51
  • Possible duplicate of [RegEx match open tags except XHTML self-contained tags](https://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags) – M. Eriksson Mar 04 '19 at 15:15
  • You should look into using something like [DOMDocument](http://php.net/manual/en/class.domdocument.php) instead of trying to parse HTML using regular expressions (since HTML is anything but regular). – M. Eriksson Mar 04 '19 at 15:16
  • DOMDocument dosen't fit to my needs , there is no way to change the input tag , it there is a way tell me – user1080247 Mar 04 '19 at 15:18
  • not a duplicate , read my question first , the case not to match the pattern , the case is to avoid adding quotation to the replacement string – user1080247 Mar 04 '19 at 15:21

1 Answers1

1

Your second capturing group captures the quotes too. Move the quotes out of the capturing group, for example by changing the pattern to

/(<input\s+).*?src="([^"]*)".*?>/is
Joni
  • 108,737
  • 14
  • 143
  • 193