如何在HTML中将某些单词更改为链接?

时间:2010-10-11 11:30:32

标签: php preg-replace str-replace

我想将列表(在文件或数据库中)中的单词更改为HTML网站中的链接。我使用str_replace,但我有替换单词的问题,已经在链接auchor。

例如。我有这样的HTML:

Lorem ipsum donor et simet <a>lorem ipsum</a> eta raoa talkesa z uta.

我想将所有“ipsum”替换为链接,但是在 [a] lorem ipsum [a] 中跳过ipsum。 我不知道,maby preg_replace?

1 个答案:

答案 0 :(得分:2)

所以我的理解是你有一个单词列表,需要在HTML体内链接。 str_replace()处理它,但不是已经在锚点内的链接?

如果匹配的单词位于锚标记内,您希望忽略它们吗?

PHP不支持可变宽度负向后观,因此不可能说不匹配在匹配单词前面有锚标记,因为头锚标记是可变长度。

我处理这类问题的方法是替换所有这些问题,然后撤消不应该进行的更改。

<?php
// Setup data
$words = array('lorem' => 'www.google.com',
               'ipsum' => 'www.bbc.co.uk',
               'test' => 'www.amazon.co.uk');

$textBody = '<p>This is a short test of <a href="www.slashdot.org">lorem ipsum</a> automatic anchoring.  Let us see if it works, any incidences of lorem or ipsum, should be caught.</p>';

// Make basic replacements, but use a different tag than anchor
// so it can be detected separately from previously existing anchors
// I am using the <argh> tag

$wordExpressions = array();
$wordReplacements = array();
foreach ($words as $cWord => $cLink) {
  $wordExpressions[] = '#' . preg_quote($cWord) . '#';
  $wordReplacements[] = '<argh href="' . $cLink . '">' . $cWord . '</argh>';
}

$replacedText = preg_replace($wordExpressions, $wordReplacements, $textBody);

// At the moment, there are nested anchors
echo $replacedText;

// Use a fairly horrific recursive anchor tag callback replacement to delete any
// <argh> tags inside <a> tags
$replacedText =
  preg_replace_callback("#(<a [^>]*>)((?:[^<]|<(?!/?a>)|(?R))+)(</a>)#",
                        create_function('$a', 'return $a[1] . preg_replace("#<argh[^>]*>(.*?)</argh>#", "$1", $a[2]) . $a[3];'),
                        $replacedText);

// No nested anchors now
echo $replacedText;

// Finally replace the <argh> tags with straight <a>s
$replacedText = preg_replace(array('#<argh #', '#</argh>#'), array('<a ', '</a>'), $replacedText);

// The output should now be correct
echo $replacedText;
?>

这看起来比它更糟糕,尤其是递归正则表达式回调。所有这一切都匹配成对的锚标签,并将匹配传递给一个简单返回修补对的函数,并从内部内容中剥离新标签。关于在"Mastering Regular Expressions" by Jeffery Friedl中使用递归替换的讨论很好。

标签可以是任何东西,我使用该单词,因为它不太可能存在于HTML中,并且似乎适合于手头的问题。 : - )

这样的事情对你有用吗?

相关问题