如何使用 PHP link HTML 中的每个单词实例?
How to link every instance of a word in HTML using PHP?
我有一个 HTML 字符串,我想找到特定单词的每个实例并将其自动 link 到页面。例如,在 HTML 字符串中找到单词 'homepage' 并将其 link 到站点的主页。
我发现以下代码片段完成了大部分逻辑:
http://aidanlister.com/2004/04/highlighting-a-search-string-in-html-text/
不过好像没有考虑到:
- 如果单词在 HTML 元素的属性中(即:img 标题属性、标签 href 属性)。这破坏了代码。
- 如果单词已经在 link 中,则不处理它(保持原样 link)。
HTML 字符串:
<h1>Hello, welcome to my site</h1>
<p>This is my site, if you want to go back to the homepage, just <a href="http://www.example.com">click here</a>.</p>
<a href="http://www.example.com" title="my homepage"><img src="/images/homepage.jpg" title="homepage screenshot" /></a>
PHP:
<?
echo str_highlight($html,'homepage','wholeword|striplinks','<a href="http://www.example.com">Homepage</a>');
?>
函数:
function str_highlight($text, $needle, $options = null, $highlight = null)
{
// Default highlighting
if ($highlight === null) {
$highlight = '<strong></strong>';
}
// Select pattern to use
if ($options & 'simple') {
$pattern = '#(%s)#';
$sl_pattern = '#(%s)#';
} else {
$pattern = '#(?!<.*?)(%s)(?![^<>]*?>)#';
$sl_pattern = '#<a\s(?:.*?)>(%s)</a>#';
}
// Case sensitivity
if (!($options & 'casesensitive')) {
$pattern .= 'i';
$sl_pattern .= 'i';
}
$needle = (array) $needle;
foreach ($needle as $needle_s) {
$needle_s = preg_quote($needle_s);
// Escape needle with optional whole word check
if ($options & 'wholeword') {
$needle_s = '\b' . $needle_s . '\b';
}
// Strip links
if ($options & 'striplinks') {
$sl_regex = sprintf($sl_pattern, $needle_s);
$text = preg_replace($sl_regex, '', $text);
}
$regex = sprintf($pattern, $needle_s);
$text = preg_replace($regex, $highlight, $text);
}
return $text;
}
替换
// Select pattern to use
if ($options & 'simple') {
$pattern = '#(%s)#';
$sl_pattern = '#(%s)#';
} else {
$pattern = '#(?!<.*?)(%s)(?![^<>]*?>)#';
$sl_pattern = '#<a\s(?:.*?)>(%s)</a>#';
}
与
if ($options & 'simple') {
$pattern = '#(%s)#';
$sl_pattern = '#(%s)#';
}
if ($options & 'html') {
$pattern = '#(?!<.*?)(%s)(?![^<>]*?>)#';
$sl_pattern = '#<a\s(?:.*?)>(%s)</a>#';
}
并像这样使用它:
str_highlight($html,'homepage','html|wholeword|striplinks','<a href="http://www.example.com">Homepage</a>');
我有一个 HTML 字符串,我想找到特定单词的每个实例并将其自动 link 到页面。例如,在 HTML 字符串中找到单词 'homepage' 并将其 link 到站点的主页。
我发现以下代码片段完成了大部分逻辑:
http://aidanlister.com/2004/04/highlighting-a-search-string-in-html-text/
不过好像没有考虑到:
- 如果单词在 HTML 元素的属性中(即:img 标题属性、标签 href 属性)。这破坏了代码。
- 如果单词已经在 link 中,则不处理它(保持原样 link)。
HTML 字符串:
<h1>Hello, welcome to my site</h1>
<p>This is my site, if you want to go back to the homepage, just <a href="http://www.example.com">click here</a>.</p>
<a href="http://www.example.com" title="my homepage"><img src="/images/homepage.jpg" title="homepage screenshot" /></a>
PHP:
<?
echo str_highlight($html,'homepage','wholeword|striplinks','<a href="http://www.example.com">Homepage</a>');
?>
函数:
function str_highlight($text, $needle, $options = null, $highlight = null)
{
// Default highlighting
if ($highlight === null) {
$highlight = '<strong></strong>';
}
// Select pattern to use
if ($options & 'simple') {
$pattern = '#(%s)#';
$sl_pattern = '#(%s)#';
} else {
$pattern = '#(?!<.*?)(%s)(?![^<>]*?>)#';
$sl_pattern = '#<a\s(?:.*?)>(%s)</a>#';
}
// Case sensitivity
if (!($options & 'casesensitive')) {
$pattern .= 'i';
$sl_pattern .= 'i';
}
$needle = (array) $needle;
foreach ($needle as $needle_s) {
$needle_s = preg_quote($needle_s);
// Escape needle with optional whole word check
if ($options & 'wholeword') {
$needle_s = '\b' . $needle_s . '\b';
}
// Strip links
if ($options & 'striplinks') {
$sl_regex = sprintf($sl_pattern, $needle_s);
$text = preg_replace($sl_regex, '', $text);
}
$regex = sprintf($pattern, $needle_s);
$text = preg_replace($regex, $highlight, $text);
}
return $text;
}
替换
// Select pattern to use
if ($options & 'simple') {
$pattern = '#(%s)#';
$sl_pattern = '#(%s)#';
} else {
$pattern = '#(?!<.*?)(%s)(?![^<>]*?>)#';
$sl_pattern = '#<a\s(?:.*?)>(%s)</a>#';
}
与
if ($options & 'simple') {
$pattern = '#(%s)#';
$sl_pattern = '#(%s)#';
}
if ($options & 'html') {
$pattern = '#(?!<.*?)(%s)(?![^<>]*?>)#';
$sl_pattern = '#<a\s(?:.*?)>(%s)</a>#';
}
并像这样使用它:
str_highlight($html,'homepage','html|wholeword|striplinks','<a href="http://www.example.com">Homepage</a>');