3

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?

5
  • 2
    instead of <?php echo ..?> try $picsrc directly, $node->setAttribute('src',$picsrc); Commented Jul 30, 2012 at 12:39
  • So you are generating PHP files dynamically? Hmm. That's tricky. Maybe create a placeholder and replace it afterwards Commented Jul 30, 2012 at 12:39
  • @GeoPhoenix Doing $node->setAttribute('src',$picsrc); don't get the needed Result because I have to insert a <?php ?>. The cause is, I have to insert a mysql query an some other Stuff there. Commented Jul 30, 2012 at 15:32
  • @Pekka Creating a Placeholder would do this Job, but I can't believe there isn't a easier way to do this. For using a Placeholder, I have to build html-file with Placeholders, loading file to string an replace the placeholders with <?php ?> Commented Jul 30, 2012 at 15:35
  • Well, this is a very special case and you're essentially looking to create invalid HTML. Arguably, a HTML builder is rightly not going to help you do that Commented Jul 30, 2012 at 15:44

2 Answers 2

1

You should try this

$srcPath = '<?php echo $picsrc;?>';
$node->setAttribute('src', html_entity_decode($srcPath));
Sign up to request clarification or add additional context in comments.

1 Comment

By doing setAttribute() the String is still unmodified. The Modification happens by building the HTML file from DOM with $dom->save($filename); So I can't docode anything on this string.
0

@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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.