我有一些纯文本和HTML.我需要创建一个PHP方法,它将返回相同的html,但<span class="marked">在文本的任何实例之前和</span>之后.
请注意,它应该支持html中的标记(例如,如果文本是blabla这样的话,它应该标记为何时bla<b>bla</b>或<a href="http://abc.com">bla</a>bla.
它应该是敏感的并且支持长文本(使用多行等).
例如,如果我使用文本"我的名字是josh"和以下html调用此函数:
<html>
<head>
<title>My Name Is Josh!!!</title>
</head>
<body>
<h1>my name is <b>josh</b></h1>
<div>
<a href="http://www.names.com">my name</a> is josh
</div>
<u>my</u> <i>name</i> <b>is</b> <span style="font-family: Tahoma;">Josh</span>.
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
......它应该返回:
<html>
<head>
<title><span class="marked">My Name Is Josh</span>!!!</title>
</head>
<body>
<h1><span class="marked">my name is <b>josh</b></span></h1>
<div>
<span class="marked"><a href="http://www.names.com">my name</a> is josh</span>
</div>
<span class="marked"><u>my</u> <i>name</i> <b>is</b> <span style="font-family: Tahoma;">Josh</span></span>.
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
谢谢.
bob*_*nce 12
这将是棘手的.
虽然你可以用简单的正则表达式黑客攻击,忽略标记内的任何东西,像天真的东西:
preg_replace(
'My(<[^>]>)*\s+(<[^>]>)*name(<[^>]>)*\s+(<[^>]>)*is(<[^>]>)*\s+(<[^>]>)*Josh',
'<span class="marked">$0</span>', $html
)
Run Code Online (Sandbox Code Playgroud)
这根本不可靠.部分原因是HTML无法使用正则表达式进行解析:放入>属性值是有效的,而其他非元素结构(如注释)将被错误解析.即使使用更严格的表达来匹配标签 - 类似于非常笨拙的东西<[^>\s]*(\s+([^>\s]+(\s*=\s*([^"'\s>][\s>]*|"[^"]*"|'[^']*')\s*))?)*\s*\/?>,您仍然会遇到许多相同的问题,特别是如果输入HTML不能保证有效.
这甚至可能是一个安全问题,就像您正在处理的HTML不受信任一样,它可能会欺骗您的解析器将文本内容转换为属性,从而导致脚本注入.
但即使忽略这一点,您也无法确保正确的元素嵌套.所以你可能会转向:
<em>My name is <strong>Josh</strong>!!!</em>
Run Code Online (Sandbox Code Playgroud)
陷入错误和无效:
<span class="marked"><em>My name is <strong>Josh</strong></span>!!!</em>
Run Code Online (Sandbox Code Playgroud)
要么:
My
<table><tr><td>name is</td></tr></table>
Josh
Run Code Online (Sandbox Code Playgroud)
这些元素不能用span包裹的地方.如果你运气不好,浏览器修正"纠正"你的无效输出可能会导致页面的一半"标记",或者弄乱页面布局.
所以你必须在解析的DOM级别而不是字符串黑客上执行此操作.您可以使用PHP解析整个字符串,处理它并重新序列化,但是如果从可访问性的角度来看它是可接受的,那么在JavaScript的浏览器端可能会更容易实现它,其中内容已被解析为DOM节点.
它仍然很难.这个问题处理文本将在同一文本节点内的位置,但这是一个更简单的情况.
你实际需要做的是:
for each Element that may contain a <span>:
for each child node in the element:
generate the text content of this node and all following siblings
match the target string/regex against the whole text
if there is no match:
break the outer loop - on to the next element.
if the current node is an element node and the index of the match is not 0:
break the inner loop - on to the next sibling node
if the current node is a text node and the index of the match is > the length of the Text node data:
break the inner loop - on to the next sibling node
// now we have to find the position of the end of the match
n is the length of the match string
iterate through the remaining text node data and sibling text content:
compare the length of the text content with n
less?:
subtract length from n and continue
same?:
we've got a match on a node boundary
split the first text node if necessary
insert a new span into the document
move all the nodes from the first text node to this boundary inside the span
break to outer loop, next element
greater?:
we've got a match ending inside the node.
is the node a text node?:
then we can split the text node
also split the first text node if necessary
insert a new span into the document
move all contained nodes inside the span
break to outer loop, next element
no, an element?:
oh dear! We can't insert a span here
Run Code Online (Sandbox Code Playgroud)
哎哟.
如果可以单独包装作为匹配项的一部分的每个文本节点,这是另一种建议,稍微不那么讨厌.所以:
<p>Oh, my</p> name <div><div>is</div><div> Josh
Run Code Online (Sandbox Code Playgroud)
会留给你输出:
<p>Oh, <span class="marked">my</span></p>
<span class="marked"> name </span>
<div><div><span class="marked">is</span></div></div>
<span class="marked"> Josh</span>
Run Code Online (Sandbox Code Playgroud)
这可能看起来不错,取决于你如何设计匹配.它还可以解决部分内部元素匹配的误解问题.
ETA:哦,这个伪代码,我现在或多或少地写了代码,不管怎么说,还可以完成它.这是后一种方法的JavaScript版本:
markTextInElement(document.body, /My\s+name\s+is\s+Josh/gi);
function markTextInElement(element, regexp) {
var nodes= [];
collectTextNodes(nodes, element);
var datas= nodes.map(function(node) { return node.data; });
var text= datas.join('');
// Get list of [startnodei, startindex, endnodei, endindex] matches
//
var matches= [], match;
while (match= regexp.exec(text)) {
var p0= getPositionInStrings(datas, match.index, false);
var p1= getPositionInStrings(datas, match.index+match[0].length, true);
matches.push([p0[0], p0[1], p1[0], p1[1]]);
}
// Get list of nodes for each match, splitted at the edges of the
// text. Reverse-iterate to avoid the splitting changing nodes we
// have yet to process.
//
for (var i= matches.length; i-->0;) {
var ni0= matches[i][0], ix0= matches[i][1], ni1= matches[i][2], ix1= matches[i][3];
var mnodes= nodes.slice(ni0, ni1+1);
if (ix1<nodes[ni1].length)
nodes[ni1].splitText(ix1);
if (ix0>0)
mnodes[0]= nodes[ni0].splitText(ix0);
// Replace each text node in the sublist with a wrapped version
//
mnodes.forEach(function(node) {
var span= document.createElement('span');
span.className= 'marked';
node.parentNode.replaceChild(span, node);
span.appendChild(node);
});
}
}
function collectTextNodes(texts, element) {
var textok= [
'applet', 'col', 'colgroup', 'dl', 'iframe', 'map', 'object', 'ol',
'optgroup', 'option', 'script', 'select', 'style', 'table',
'tbody', 'textarea', 'tfoot', 'thead', 'tr', 'ul'
].indexOf(element.tagName.toLowerCase()===-1)
for (var i= 0; i<element.childNodes.length; i++) {
var child= element.childNodes[i];
if (child.nodeType===3 && textok)
texts.push(child);
if (child.nodeType===1)
collectTextNodes(texts, child);
};
}
function getPositionInStrings(strs, index, toend) {
var ix= 0;
for (var i= 0; i<strs.length; i++) {
var n= index-ix, l= strs[i].length;
if (toend? l>=n : l>n)
return [i, n];
ix+= l;
}
return [i, 0];
}
// We've used a few ECMAScript Fifth Edition Array features.
// Make them work in browsers that don't support them natively.
//
if (!('indexOf' in Array.prototype)) {
Array.prototype.indexOf= function(find, i /*opt*/) {
if (i===undefined) i= 0;
if (i<0) i+= this.length;
if (i<0) i= 0;
for (var n= this.length; i<n; i++)
if (i in this && this[i]===find)
return i;
return -1;
};
}
if (!('forEach' in Array.prototype)) {
Array.prototype.forEach= function(action, that /*opt*/) {
for (var i= 0, n= this.length; i<n; i++)
if (i in this)
action.call(that, this[i], i, this);
};
}
if (!('map' in Array.prototype)) {
Array.prototype.map= function(mapper, that /*opt*/) {
var other= new Array(this.length);
for (var i= 0, n= this.length; i<n; i++)
if (i in this)
other[i]= mapper.call(that, this[i], i, this);
return other;
};
}
Run Code Online (Sandbox Code Playgroud)