Robin Fleischer
Robin Fleischer

Reputation: 31

How to Add PHP-Code to an attribute using DOM on PHP?

How can I do somethink like this with PHP DOM?

<img src="<?php echo $picsrc; ?>">

This Code

$node->setAttribute('src','<?php echo $picsrc;?>');

does escape the PHP Tag.

<img src="&lt;?php echo $picsrc; ?&gt;">

Is there a way to use

$dom->createProcessingInstruction()

for Attribute Values?

Upvotes: 3

Views: 252

Answers (2)

Robin Fleischer
Robin Fleischer

Reputation: 31

@thecatontheflat was right, decoding is the Answer!

But using it this way:

$node->setAttribute('src','<?php echo $picsrc; ?>');

$doc->save('dynamic.php');
$html = htmlspecialchars_decode(file_get_contents('dynamic.php'));
$html = html_entity_decode($html);
file_put_contents('dynamic.php',$html);

Don't try to avert the conversion, just revoke it. ;)

thanks at all.

Upvotes: 0

Vitalii Zurian
Vitalii Zurian

Reputation: 17976

You should try this

$srcPath = '<?php echo $picsrc;?>';
$node->setAttribute('src', html_entity_decode($srcPath));

Upvotes: 1

Related Questions