【问题标题】:Domdocument saveHTML() adding extra quotes and some other url encoded charactersDomdocument saveHTML() 添加额外的引号和其他一些 url 编码字符
【发布时间】:2011-08-19 05:12:54
【问题描述】:

我一直在使用 PHP 的 Domdocument 扩展来查找没有 alt 属性或空 alt 属性的图像标签。这是我用于测试目的的 html 代码:

<span style="font-weight:bold;">Blender</span> is an Open Source 3D modelling and animation software. 
This is a very popular software among hobbyists.<i>Blender</i> has a vast list of features which include bones and meshing, textures, particle physics etc.
<u>Blender</u> was originally a proprietary software which was eventually made opensource. 
Blender is known to be difficult to learn because its interface is very intimiding to a newbie. 
But on the other hand, <a href="http://www.blender.org">Blender</a> is so much customizable that you can actually modify your workspace according to your personal preference. 
Also blender interface has been developed in the OpenGL graphics library, so blender looks all the same on all platforms whether you use Windows, Linux, BSD or even Mac. 
3D is a very interesting field to work with but 3D is somewhat tough to start with. You can <a href="http://www.google.com"" target="_blank">Google</a> for numerous tutorials on Blender. 
There are quite some awesome websites dedicated to blender development, such as BlenderGuru.com. <img src="http://www.cochinsquare.com/wp-content/uploads/2010/08/Blender.jpg">

这是我用来搜索 IMG 标记并向其添加 alt 属性的 Domdocument 代码。

$dom=new DOMDocument();
$dom->loadHTML($content);
$dom->formatOutput = true;
$imgs = $dom->getElementsByTagName("img");
foreach($imgs as $img){
 $alt = $img->getAttribute('alt');
 if ($alt == ''){
  $k_alt = $this->keyword;    
 }else{
  $k_alt = $alt;
 }
 $img->setAttribute( 'alt' , $k_alt );
}
$html_mod = preg_replace('/^<!DOCTYPE.+?>/', '', str_replace( array('<html>', '</html>', '<body>', '</body>'), array('', '', '', ''), $dom->saveHTML()));
return $html_mod;

这是我得到的 html。

<span style='"font-weight:bold;"'>Blender</span> is an Open Source 3D modelling and animation software. 
This is a very popular software among hobbyists.<i>Blender</i> has a vast list of features which include bones and meshing, textures, particle physics etc.
<u>Blender</u> was originally a proprietary software which was eventually made opensource. 
Blender is known to be difficult to learn because its interface is very intimiding to a newbie. 
But on the other hand, <a href=""http://www.blender.org"">Blender</a> is so much customizable that you can actually modify your workspace according to your personal preference. 
Also blender interface has been developed in the OpenGL graphics library, so blender looks all the same on all platforms whether you use Windows, Linux, BSD or even Mac. 
3D is a very interesting field to work with but 3D is somewhat tough to start with. You can <a href=""http://www.google.com""" target='"_blank"'>Google</a> for numerous tutorials on Blender. 
There are quite some awesome websites dedicated to blender development, such as BlenderGuru.com. 
<img src=""http://www.cochinsquare.com/wp-content/uploads/2010/08/Blender.jpg"" alt="Blender">

观察img src和anchor标签以及span的style属性中多余的引号(单引号和双引号)。

请帮忙!我希望只添加新的 alt 属性,原封不动地返回 html。

另外我想提一下,我在 Ubuntu 10.04 上使用 PHP 5.3.2 和 Suhosin 补丁

【问题讨论】:

  • 我无法在 PHP/5.3.6 for Windows 上重现该问题。由于无效的 HTML (href="http://www.google.com""),我在 loadHTML() 收到警告,但输出是正确的。错误是否有可能在其他地方?您的代码似乎被剥离了一个类:-?
  • 没关系。我采用了正则表达式。 :) 你是对的,我在课堂上使用代码。顺便说一句,我也在使用 64 位的 ubuntu 系统。可能和这个问题有关

标签: php domdocument


【解决方案1】:

我终于想出了如何解决这个问题,并想与你分享我的解决方案 为了避免在 saveHtml 之后添加引号,您应该在 saveHTML 函数的结果上使用 html_entity_decode 例如:

$filecontent = file_get_contents('file.html');
$doc = new DOMDocument();
$doc->loadHTML($filecontent);
$xpath = new DOMXpath($doc);
$xpath->query("//*[id='bg']")[0]->nodeValue = 'asd';
$filecontent = html_entity_decode($doc->saveHTML());
file_put_contents('file.html', $file_contents);

所以你会在 $filecontent 变量中得到很好的正确的 html 代码,没有多余的引号 不客气!

【讨论】:

    猜你喜欢
    • 2015-02-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-29
    • 1970-01-01
    • 2011-06-15
    • 1970-01-01
    • 2021-10-19
    相关资源
    最近更新 更多