How to setAttribute of DOMDocument element with special characters?

https://stackoverflow.com/questions/37174897/how-to-setattribute-of-domdocument-element-with-special-characters

Ask Questionarrow-up-rightAsked 4 years, 11 months agoActive 3 months agoarrow-up-rightViewed 2k times3

In an HTML block such as this:

<p>Hello: <img src="hello/foo.png" /></p>

I need to transform the URL src of the image to a Laravel storage_path link. I'm using PHP DOMDocument to transform the url like so:

$link = $a->getAttribute('src');
$pat = '#(\w+\.\w+)+(?!.*(\w+)(\.\w+)+)#';
$matches = [];
preg_match($pat, $link, $matches);
$newStr = "{{ storage_path('app/' . " . $matches[0] . ") }}";
$a->setAttribute('src', $newStr);

The problem is that the output is src="%7B%7B%20storage_path('app/'%20.%20foo.png)%20%7D%7D"

How can I keep the special characters of the src attribute?phparrow-up-rightdomdocumentarrow-up-rightsetattributearrow-up-rightSharearrow-up-rightImprove this questionarrow-up-rightFollowasked May 11 '16 at 23:31arrow-up-rightgreenerarrow-up-right4,7351010 gold badges4545 silver badges8383 bronze badges

Add a commentarrow-up-right

1 Answer

Activearrow-up-rightOldestarrow-up-rightVotesarrow-up-right3

You can use something like:

Last updated