Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在PHP上使用DOM向属性添加PHP代码?_Php_Dom_Attributes - Fatal编程技术网

如何在PHP上使用DOM向属性添加PHP代码?

如何在PHP上使用DOM向属性添加PHP代码?,php,dom,attributes,Php,Dom,Attributes,如何使用PHPDOM进行类似的思考 <img src="<?php echo $picsrc; ?>"> 对于属性值?您应该试试这个 $srcPath = '<?php echo $picsrc;?>'; $node->setAttribute('src', html_entity_decode($srcPath)); $srcPath=''; $node->setAttribute('src',html_entity_decode($srcPath

如何使用PHPDOM进行类似的思考

<img src="<?php echo $picsrc; ?>">
对于属性值?

您应该试试这个

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

@密码是正确的,解码就是答案

但用这种方式:

$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);
$node->setAttribute('src','');
$doc->save('dynamic.php');
$html=htmlspecialchars\u decode(文件获取内容('dynamic.php');
$html=html\u实体\u解码($html);
文件内容('dynamic.php',$html);
不要试图避免转换,只要撤销它即可


非常感谢。

不要直接尝试
$picsrc
$node->setAttribute('src',$picsrc),而要尝试
$picsrc
那么您是在动态生成PHP文件?嗯,这很棘手。可能创建一个占位符并替换它afterwards@GeoPhoenix执行$node->setAttribute('src',$picsrc);没有得到所需的结果,因为我必须插入一个。原因是,我必须在那里插入一个mysql查询和一些其他东西。@Pekka创建一个占位符可以完成这项工作,但我不相信没有更简单的方法来完成这项工作。对于使用占位符,我必须用占位符构建html文件,加载文件以字符串形式替换占位符,这是一个非常特殊的情况,您实际上是在创建无效的html。可以说,HTML构建器不会通过执行setAttribute()来帮助您做到这一点,因为字符串仍然没有修改。修改是通过使用$DOM->save($filename)从DOM构建HTML文件来实现的;所以我不能对这个字符串进行任何文档编码。
$dom->createProcessingInstruction()
$srcPath = '<?php echo $picsrc;?>';
$node->setAttribute('src', html_entity_decode($srcPath));
$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);