是否可以使用 DOMDocument 类并且不允许它添加文档类型声明、head 和 body 标签?我正在为服务器端包含编写我当前的代码,并且它正在一个已经格式良好的页面上呈现。我不需要额外的标签。
我有这个html片段:
<font color="#ff0000">Lorem <font size="4">ipsum dolor</font> sit amet</font>
Run Code Online (Sandbox Code Playgroud)
我想使用DOMDocument font用a 替换每个标记span.这是我的功能atm:
$fonts = $xPath->query('//font');
foreach($fonts as $font){
$style = '';
$newFont = $dom->createElement('span',$font->nodeValue);
if($font->hasAttribute('size')){
$size = $font->getAttribute('size');
$style.='font-size:'.round($size/2,1).'em; ';
}
if($font->hasAttribute('color')){
$style.='color:'.$font->getAttribute('color').'; ';
}
if($style!='') $newFont->setAttribute('style',$style);
$font->parentNode->replaceChild($newFont,$font);
}
Run Code Online (Sandbox Code Playgroud)
我期待这个输出:
<span style="color:#ff0000; ">Lorem <span style="font-size:2em;">ipsum etc..
Run Code Online (Sandbox Code Playgroud)
但我得到:
<span style="color:#ff0000; ">Lorem ipsum dolor sit amet</span>
Run Code Online (Sandbox Code Playgroud)
为什么?
我想这$font->parentNode->replaceChild($newFont,$font);是因为以某种方式用它的文本值替换外部跨度......或者这个查询可能$xPath->query('//font')是错误的.我很喜欢经验丰富的建议......谢谢
如何迭代所有标签并检查类是 font18 还是 font17?
\n\n $html = new DOMDocument();\n $html->load(\'file.html\');\nRun Code Online (Sandbox Code Playgroud)\n\nhtml:
\n\n <p><a name="bookmark7"></a><span class="font18" style="font-weight:bold;">Abilitazione</span></p>\n<p><span class="font17">I medici devono essere autorizzati dallo Stato a praticare la loro professione. I requisiti per ottenere questa autorizzazione variano a seconda delle diverse Nazioni. I laureati presso Facolt\xc3\xa0 mediche estere possono ottenere l\'autorizzazione a esercitare in Italia se rispondono ai requisiti statali per quanto riguarda il tirocinio e se superano l\'esame di Stato. Nell\'ambito della CEE si tratta tuttora di una questione da …Run Code Online (Sandbox Code Playgroud) 我在php中解析html,因为我无法控制原始内容,我想删除样式和不必要的标签,同时仍保留内容和标签的简短列表,即:
p,img,iframe(也许还有其他一些)
我知道我可以删除一个给定的标签(参见我在下面使用的代码),但由于我不一定知道它们可能是什么标签,而且我不想创建一个巨大的可能列表,我会喜欢除了我允许的列表之外能够删除所有内容.
function DOMRemove(DOMNode $from) {
$sibling = $from->firstChild;
do {
$next = $sibling->nextSibling;
$from->parentNode->insertBefore($sibling, $from);
} while ($sibling = $next);
$from->parentNode->removeChild($from);
}
$dom = new DOMDocument;
$dom->loadHTML($html);
$nodes = $dom->getElementsByTagName('span');
Run Code Online (Sandbox Code Playgroud) 当我将 xmlns 添加到我用 DOMDocument 编写的 xml 时,我不断收到以下错误
致命错误:未捕获的异常“DOMException”,消息“无效字符错误”...
$xml = new DOMDocument("1.0", "UTF-8");
$xml_urlset = $xml->createElement('urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"');
$xml_url = $xml->createElement("url","this text");
$xml_urlset->appendChild($xml_url);
$xml->appendChild($xml_urlset);
$xml->formatOutput = true;
$xml->preserveWhiteSpace = false;
$xml->save("test.xml");
Run Code Online (Sandbox Code Playgroud)
此外,即使我有 formatOutput = true 我仍然把所有内容写成一行:
<urlset>xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd"<url>this text</url></urlset>
Run Code Online (Sandbox Code Playgroud)
我试图设置一些东西,以便可以为 urlset 输出
<urlset
xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9
http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">
Run Code Online (Sandbox Code Playgroud)
谢谢你能给我的任何帮助。
我注意到在这个网址上:http : //www.bubbleroom.se/sv/kläder/kvinna/controlbody/bodys/body-nero\u0标签中有一个空字符id prodText。
尝试在此字符之后提取任何其他内容时,整个文档似乎以此空字符结尾。
“不起作用”的代码。它有效,但当$html字符串中有空字符时无效
$dom = new DOMDocument;
libxml_use_internal_errors(true);
$dom->loadHTML($html);
libxml_clear_errors();
return new DOMXPath($dom);
Run Code Online (Sandbox Code Playgroud) 我有 2 个“DOMDocument”对象 - $original 和 $additional。我想要的是从 $additional DOMDocument 中取出所有孩子并将其附加到 $original 文档的末尾。
我的计划是获取 $additional 文档的根元素。我尝试使用:
$root = $additional->documentElement;
$original->appendChild($root)
Run Code Online (Sandbox Code Playgroud)
但是我收到 appendChild 期望 DOMNode 对象作为参数的错误。我尝试通过以下方式访问文档的每个子项:
$additional->childNodes->item(0);
Run Code Online (Sandbox Code Playgroud)
但它返回 DOMElement 的对象。你能建议如何获取 DOMNode 类的对象吗?提供此导入操作的最方便的方法是什么?
$original XML 看起来像:
<?xml version="1.0" encoding="utf-8"?>
<Product>
<RecordReference>345345</RecordReference>
<NotificationType>03</NotificationType>
<NumberOfPages>100</NumberOfPages
</Product>
Run Code Online (Sandbox Code Playgroud)
$additional XML 看起来像:
<?xml version="1.0" encoding="utf-8"?>
<MainSubject>
<SubjectScheme>10</SubjectScheme>
</MainSubject>
Run Code Online (Sandbox Code Playgroud)
我想要的:
<?xml version="1.0" encoding="utf-8"?>
<Product>
<RecordReference>345345</RecordReference>
<NotificationType>03</NotificationType>
<NumberOfPages>100</NumberOfPages>
<MainSubject>
<SubjectScheme>10</SubjectScheme>
</MainSubject>
</Product>
Run Code Online (Sandbox Code Playgroud) 我希望我能从你们那里得到一些帮助。
这就是我正在努力解决的问题,我有一串 HTML 看起来像这样:
<h4>Some title here</h4>
<p>Lorem ipsum dolor</p>
(some other HTML here)
<h4>Some other title here</h4>
<p>Lorem ipsum dolor</p>
(some other HTML here)
Run Code Online (Sandbox Code Playgroud)
我需要将所有<h4>内容与其余内容分开,但例如第一个之后<h4>和第二个之前的内容<h4>需要与第一个相关<h4>,如下所示:
Array {
[0] => <h4>Some title here</h4>
[1] => <p>Lorem ipsum dolor</p>
}
Array {
[0] => <h4>Some other title here</h4>
[1] => <p>Lorem ipsum dolor</p>
}
Run Code Online (Sandbox Code Playgroud)
这是为了构建一个手风琴(很难解释我为什么要这样做,但它必须是这样),并且<h4>将是手风琴面板标题,单击时它将展开并显示与它们相关的内容。
我希望我说清楚了我的问题,让我知道你的想法以及我应该如何更好地做到这一点。
我正在研究DOMDocument,但我也尝试过explode()但没有成功。
我使用 JavaScript 进行了这项工作,但我需要使用 PHP 实现相同的功能,但是使用 PHP 使用 DOM 非常复杂。
先感谢您。
我发现这个帖子很好地描述了我的问题,这个答案完全描述了我的问题.
非中断空格字符是字节0xA0是ISO-8859-1; 当编码为UTF-8时,它是0xC2,0xA0,如果您(错误地)将其视为ISO-8859-1,则表示为
"Â ".这包括一个尾随......
但是,我设法将我的问题跟踪到我用来在div中包装图像标签的函数.
function img_format($str)
{
$doc = new DOMDocument();
@$doc->loadHTML($str); // <-- Bonus points for the explaination of the @
// $tags object
$tags = $doc->getElementsByTagName('img');
foreach ($tags as $tag) {
$div = $doc->createElement('div');
$div->setAttribute('class','inner-copy');
$tag->parentNode->insertBefore($div, $tag);
$div->appendChild($tag);
$tag->setAttribute('class', 'inner-img');
}
$str = $doc->saveHTML();
return $str;
}
Run Code Online (Sandbox Code Playgroud)
很简单,我如何在此功能中解决此问题?
我理解使用;
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
Run Code Online (Sandbox Code Playgroud)
将解决这个问题,但显然我在功能本身内忽略了一些东西.
我试过了;
$dom->validateOnParse = true;
Run Code Online (Sandbox Code Playgroud)
无济于事.(我不知道那是怎么回事)
为什么我会收到此代码的警告?
$content ='<p>
<a href="http://www.we.com/1000">text </a>
text
<a href="http://www.we.com/2345">text </a>
text
</p>
<p>text</p>
<p>
<table border="1" cellpadding="0" cellspacing="0" dir="rtl">
<tbody>
<tr>
<td>text </td>
<td>text </td>
<td>text </td>
</tr>
<tr>
<td>text </td>
<td>text </td>
<td>text </td>
</tr>
</tbody>
</table>
</p>';
$doc = new DOMDocument('1.0', 'UTF-8');
$doc->loadHTML($content);
Run Code Online (Sandbox Code Playgroud)
警告是:
警告:DOMDocument :: loadHTML():意外结束标记:实体中的p,第2213行的/home/admin/domains/we.com/public_html/refresh/lib/core.php中的第25行<p> <a href =“ http://www.we.com/1000 ” > 文本 </a>文本<a href=" http://www.we.com/2345">文本 </a>文本</ p> < p> text </ p> <p> </ p> <table border =“ 1” cellpadding =“ 0” cellspacing =“ 0” dir …
domdocument ×10
php ×10
dom ×3
html ×2
xml ×2
accordion ×1
arrays ×1
doctype ×1
domxpath ×1
html-parsing ×1
php-parser ×1