我是下面的函数,我在努力输出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) 我正在html页面上创建一个preg_replace.我的模式旨在为html中的某些单词添加周围标记.但是,有时我的正则表达式会修改html标记.例如,当我尝试替换此文本时:
<a href="example.com" alt="yasar home page">yasar</a>
Run Code Online (Sandbox Code Playgroud)
这样yasar读取<span class="selected-word">yasar</span>,我的正则表达式也替换了锚标记的alt属性中的yasar.preg_replace()我正在使用的电流看起来像这样:
preg_replace("/(asf|gfd|oyws)/", '<span class=something>${1}</span>',$target);
Run Code Online (Sandbox Code Playgroud)
如何制作正则表达式,使其与html标签内的任何内容都不匹配?
对于另一个问题,我创建了一些与我的开发机器相关的XML相关代码,但在viper键盘上没有,我在测试之前将其添加到我的答案中.
我可以将我的问题减少到DOMXPath::query()我的系统和键盘之间返回的节点顺序不同.
XML: <test>This is some <span>text</span>, fine.</test>
当我查询所有文本节点时//child::text(),结果不同:
#0: This is some
#1: , fine.
#2: text
Run Code Online (Sandbox Code Playgroud)
我的机器:
#0: This is some
#1: text
#2: , fine.
Run Code Online (Sandbox Code Playgroud)
我不熟悉xpath,我确实理解为什么会发生这种情况以及如何使用PHP实现来影响返回顺序.
编辑:
进一步测试显示LIBXML_VERSION两个系统之间存在差异:
Viper Codepad: 20626 (2.6.26; 6 Jun 2006)
My Machine...: 20707 (2.7.7; 15 Mar 2010)
Run Code Online (Sandbox Code Playgroud) 我需要为元素属性动态构造XPath查询,其中属性值由用户提供.我不确定如何清理或清理此值以防止XPath等同于SQL注入攻击.例如(在PHP中):
<?php
function xPathQuery($attr) {
$xml = simplexml_load_file('example.xml');
return $xml->xpath("//myElement[@content='{$attr}']");
}
xPathQuery('This should work fine');
# //myElement[@content='This should work fine']
xPathQuery('As should "this"');
# //myElement[@content='As should "this"']
xPathQuery('This\'ll cause problems');
# //myElement[@content='This'll cause problems']
xPathQuery('\']/../privateElement[@content=\'private data');
# //myElement[@content='']/../privateElement[@content='private data']
Run Code Online (Sandbox Code Playgroud)
特别是最后一个让人想起昔日的SQL注入攻击.
现在,我知道会有包含单引号和包含双引号的属性的属性.由于这些是作为函数的参数提供的,因此对这些函数进行消毒的理想方法是什么?
我正在为我的网站创建一个搜索脚本.但我想italic在每个搜索结果的描述中匹配的单词.我正在使用的脚本在PHP,描述段落有变量$search_desc.目前我正在使用str_ireplace()功能来实现我的目标.但我认为我的目标太遥远了!这是功能
echo str_ireplace($search_query,"<i>".$search_query."</i>",$search_desc);
Run Code Online (Sandbox Code Playgroud)
但问题是如果搜索查询是,search并且例如搜索结果的描述是,
这是搜索演示的描述.
所以在将italic属性添加到它显示的匹配单词后的描述
这是搜索演示的描述.
所以你可以看到问题很严重.因为原始描述被编辑!所以任何人都对我的问题有任何想法?如果你让我知道,请
我有一个PHP突出显示功能,使某些单词变粗.
下面是函数,它很有用,除非数组:$ words包含单个值:b
例如,有人搜索:jessie j price tag feat bob
这将在数组$ words中包含以下条目:jessie,j,price,tag,feat,b,o,b
当'b'出现时,我的整个函数出错了,它显示了一大堆错误的html标签.当然,我可以从数组中去掉任何'b'值,但这并不理想,因为突出显示不适用于某些查询.
此示例脚本:
function highlightWords2($text, $words)
{
$text = ($text);
foreach ($words as $word)
{
$word = preg_quote($word);
$text = preg_replace("/\b($word)\b/i", '<b>$1</b>', $text);
}
return $text;
}
$string = 'jessie j price tag feat b o b';
$words = array('jessie','tag','b','o','b');
echo highlightWords2($string, $words);
Run Code Online (Sandbox Code Playgroud)
将输出:
<<<b>b</b>><b>b</b></<b>b</b>>>jessie</<<b>b</b>><b>b</b></<b>b</b>>> j price <<<b>b</b>><b>b</b></<b>b</b>>>tag</<<b>b</b>><b>b</b></<b>b</b>>> feat <<b>b</b>><b>b</b></<b>b</b>> <<b>b</b>>o</<b>b</b>> <<b>b</b>><b>b</b></<b>b</b>>
Run Code Online (Sandbox Code Playgroud)
这只会发生,因为数组中有"b".
你能看到我能改变的任何东西让它正常工作吗?
我有这样的几行代码
<p> <inset></p>
Run Code Online (Sandbox Code Playgroud)
<p>如果字符串,在开始标记和其余部分之间可能有任意数量的空格或制表符(或没有).我需要更换这些,但我不能让它工作.
我以为这会做到,但它不起作用:
<p>[ \t]+<inset></p>
Run Code Online (Sandbox Code Playgroud)