PHP Token 替换 html 实体 [英] PHP Token replaces html entities
问题描述
如果在文本中找到某些单词/字符串,我想制作链接之类的.我有一段来自 php.bet 的代码可以做到这一点,但它也从 <a href="http://www.domain.com/index.php" title= 中删除了标签的开头和结尾主页">转到主页
.你能帮忙解决这个问题吗?
I want to make certain words/strings like links if found in the text. I have a piece of code from php.bet which does that, but it also removes the beginning and end of tags from <a href="http://www.domain.com/index.php" title="Home">go to homepage</a>
. Can you help solve this?
代码如下:
<?php
$str_in = '<p>Hi there worm! You have a disease!</p><a href="http://www.domain.com/index.php" title="Home">go to homepage</a>';
$replaces= array(
'worm' => 'http://www.domain.com/index.php/worm.html',
'disease' => 'http://www.domain.com/index.php/disease.html'
);
function addLinks($str_in, $replaces)
{
$str_out = '';
$tok = strtok($str_in, '<>');
$must_replace = (substr($str_in, 0, 1) !== '<');
while ($tok !== false) {
if ($must_replace) {
foreach ($replaces as $tag => $href) {
if (preg_match('/\b' . $tag . '\b/i', $tok)) {
$tok = preg_replace(
'/\b(' . $tag . ')\b/i',
'<a title="' . $tag . '" href="' . $href . '">\1</a>',
$tok,
1);
unset($replaces[$tag]);
}
}
} else {
$tok = "<$tok>";
}
$str_out .= $tok;
$tok = strtok('<>');
$must_replace = !$must_replace;
}
return $str_out;
}
echo addLinks($str_in, $replaces);
结果是:
蠕虫!你有病!
a href="http://www.domain.com/index.php" title="首页"/a
a href="http://www.domain.com/index.php" title="Home"/a
将蠕虫"和疾病"词转换为所需的链接,但其余...
The "worm" and "disease" words are transformed into links like desired, but the rest...
非常感谢!
推荐答案
这对函数应该可以做你想做的没有用正则表达式解析 HTML 带来的问题 或 str_replace代码>.
This pair of functions should do what you want without the problems that come with parsing HTML with regex or str_replace
.
function process($node, $replaceRules)
{
if($node->hasChildNodes()) {
$nodes = array();
foreach ($node->childNodes as $childNode) {
$nodes[] = $childNode;
}
foreach ($nodes as $childNode) {
if ($childNode instanceof DOMText) {
$text = preg_replace(
array_keys($replaceRules),
array_values($replaceRules),
$childNode->wholeText);
$node->replaceChild(new DOMText($text),$childNode);
}
else {
process($childNode, $replaceRules);
}
}
}
}
function addLinks($str_in, $replaces)
{
$replaceRules = array();
foreach($replaces as $k=>$v) {
$k = '/\b(' . $k . ')\b/i';
$v = '<a href="' . $v . '">$1</a>';
$replaceRules[$k] = $v;
}
$doc = new DOMDocument;
$doc->loadHTML($str_in);
process($doc->documentElement, $replaceRules);
return html_entity_decode($doc->saveHTML());
}
注意:如果 HTML 结构不合理,无需担心(如您的示例所示);但是,输出将结构良好.
Note: No need to worry if the HTML is not well structured (as in your example); however, the output will be well structured.
应得的信用:递归 process()
函数执行大部分实际工作,直接来自 Lukáš Lalinský 对 如何替换 HTML 中的文本.addLinks()
函数只是针对您的问题量身定制的用例.
Credit where it’s due:
The recursive process()
function, which does most of the real work, comes direclty from Lukáš Lalinský’s answer to How to replace text in HTML. The addLinks()
function is just a use case tailored to fit your question.
这篇关于PHP Token 替换 html 实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!