我是下面的函数,我在努力输出DOMDocument而没有在内容输出之前附加XML,HTML,body和p标签包装器.建议的修复:
$postarray['post_content'] = $d->saveXML($d->getElementsByTagName('p')->item(0));
Run Code Online (Sandbox Code Playgroud)
仅在内容中没有块级元素时才有效.但是,当它执行时,如下面的例子中的h1元素,saveXML的结果输出被截断为...
<p>如果你喜欢</ p>
我已经指出这篇文章可能是一种解决方法,但是我无法理解如何将它实现到这个解决方案中(参见下面的注释).
有什么建议?
function rseo_decorate_keyword($postarray) {
global $post;
$keyword = "Jasmine Tea"
$content = "If you like <h1>jasmine tea</h1> you will really like it with Jasmine Tea flavors. This is the last ocurrence of the phrase jasmine tea within the content. If there are other instances of the keyword jasmine tea within the text what happens to jasmine tea."
$d = new DOMDocument();
@$d->loadHTML($content);
$x = new DOMXpath($d);
$count = …Run Code Online (Sandbox Code Playgroud) 我一直在尝试解析HTML5代码,所以我可以在代码中设置属性/值,但似乎DOMDocument(PHP5.3)不支持像<nav>和的标签<section>.
有没有办法在PHP中解析这个HTML并操纵代码?
代码重现:
<?php
$dom = new DOMDocument();
$dom->loadHTML("<!DOCTYPE HTML>
<html><head><title>test</title></head>
<body>
<nav>
<ul>
<li>first
<li>second
</ul>
</nav>
<section>
...
</section>
</body>
</html>");
Run Code Online (Sandbox Code Playgroud)
错误
警告:DOMDocument :: loadHTML():实体中的标签导航无效,第17行/home/wbkrnl/public_html/new-mvc/1.php中的第4行:
警告:DOMDocument :: loadHTML():实体中的标记部分无效,第17行/home/wbkrnl/public_html/new-mvc/1.php中的第10行:
我需要解析一些HTML文件,但是,它们格式不正确并且PHP打印出警告.我想以编程方式避免这种调试/警告行为.请指教.谢谢!
码:
// create a DOM document and load the HTML data
$xmlDoc = new DomDocument;
// this dumps out the warnings
$xmlDoc->loadHTML($fetchResult);
Run Code Online (Sandbox Code Playgroud)
这个:
@$xmlDoc->loadHTML($fetchResult)
Run Code Online (Sandbox Code Playgroud)
可以抑制警告,但我如何以编程方式捕获这些警告?
以下是代码:
$doc = new DomDocument('1.0');
// create root node
$root = $doc->createElement('root');
$root = $doc->appendChild($root);
$signed_values = array('a' => 'eee', 'b' => 'sd', 'c' => 'df');
// process one row at a time
foreach ($signed_values as $key => $val) {
// add node for each row
$occ = $doc->createElement('error');
$occ = $root->appendChild($occ);
// add a child node for each field
foreach ($signed_values as $fieldname => $fieldvalue) {
$child = $doc->createElement($fieldname);
$child = $occ->appendChild($child);
$value = $doc->createTextNode($fieldvalue);
$value = $child->appendChild($value);
} …Run Code Online (Sandbox Code Playgroud) 我正试图检测我的document身高何时发生变化.一旦它,我需要运行一些功能来帮助组织我的页面布局.
我不是在找window.onresize.我需要整个文档,它比窗口大.
我如何观察这种变化?
我无法理解为什么我们在PHP中需要2个XML解析器.
有人可以解释这两者之间的区别吗?
网络服务器使用utf-8编码提供响应,所有文件都使用utf-8编码保存,我所知道的设置都已设置为utf-8编码.
这是一个快速程序,用于测试输出是否有效:
<?php
$html = <<<HTML
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Test!</title>
</head>
<body>
<h1>? Hello ? World ?</h1>
</body>
</html>
HTML;
$dom = new DomDocument("1.0", "utf-8");
$dom->loadHTML($html);
header("Content-Type: text/html; charset=utf-8");
echo($dom->saveHTML());
Run Code Online (Sandbox Code Playgroud)
该计划的输出是:
<!DOCTYPE html>
<html><head><meta charset="utf-8"><title>Test!</title></head><body>
<h1>☆ Hello ☆ World ☆</h1>
</body></html>
Run Code Online (Sandbox Code Playgroud)
其呈现为:
我能做错什么?我需要告诉DomDocument正确处理utf-8还有多少具体内容?
我构建了一个脚本,它将页面上的所有css组合在一起,以便在我的cms中使用它.它工作了很长时间,现在我得到这个错误:
警告:DOMDocument :: loadHTML()[domdocument.loadhtml]:实体中的标签头无效,第26行的css.php中的第10 行警告:DOMDocument :: loadHTML()[domdocument.loadhtml]:实体中的标签导航无效,第26行的css.php中的第10 行警告:DOMDocument :: loadHTML()[domdocument.loadhtml]:实体中的标记部分无效,第26行的css.php中的第 22行:这是php脚本
这是我的代码:
<?php
header('Content-type: text/css');
include ('../global.php');
if ($usetpl == '1') {
$client = New client();
$tplname = $client->template();
$location = "../templates/$tplname/header.php";
$page = file_get_contents($location);
} else {
$page = file_get_contents('../index.php');
}
class StyleSheets extends DOMDocument implements IteratorAggregate
{
public function __construct ($source)
{
parent::__construct();
$this->loadHTML($source);
}
public function getIterator ()
{
static …Run Code Online (Sandbox Code Playgroud) 我想使用XPath href从a-tag 获取属性,但它在同一个文件中有两次出现.我怎么相处?我需要检查如果有一个href值为$ street/object 的属性,我有这个代码,它不起作用:
$product_photo = $xpath->query("//a[contains(@href,'{$object_street}fotos/')][1]");
$product_360 = $xpath->query("//a[contains(@href,'{$object_street}360-fotos/')][1]");
$product_blueprint = $xpath->query("//a[contains(@href,'{$object_street}plattegrond/')][1]");
$product_video = $xpath->query("//a[contains(@href,'{$object_street}video/')][1]");
Run Code Online (Sandbox Code Playgroud)
它根本不会返回任何东西.谁能帮助我?
是否可以从加载中删除元素DOM而不创建新元素?例如这样的事情:
$dom = new DOMDocument('1.0', 'utf-8');
$dom->loadHTML($html);
foreach($dom->getElementsByTagName('a') as $href)
if($href->nodeValue == 'First')
//delete
Run Code Online (Sandbox Code Playgroud) domdocument ×10
php ×8
html ×2
html5 ×2
javascript ×1
simplexml ×1
utf-8 ×1
warnings ×1
xml ×1
xpath ×1