如何在不替换超链接 URL、图像标签 URL、图像标签标题和 alt 标签中找到的关键字的情况下替换字符串中出现的所有$keyword?
例:
$keywords = 'sports';
$string = '<a href="http://my_domain_name.com/sports/info.php"><img class="icon" src="http://my_domain_name.com/sports/images/football.gif" title="Get the latest football sports news" alt="Get the latest football sports news" />Football sports news</a>';
请注意,关键字"sports"与超链接 URL、图像标记 URL 以及图像标记标题和 alt 标记一起显示。
我想将$keywords(体育)替换为:
<span style="color: #000000; background-color: #FFFF00; font-weight: normal;">sports</span>
大喊以下结果:
<a href="http://my_domain_name.com/sports/info.php"><img class="icon" src="http://my_domain_name.com/sports/images/football.gif" title="Get the latest football sports news" alt="Get the latest football sports news" />Football <span style="color: #000000; background-color: #FFFF00; font-weight: normal;">sports</span> news</a>
提前谢谢。
编辑 - 附加信息
目前我使用以下 2 步方法,它仅适用于 URL,而不适用于标题和 alt 标签。我也不需要替换标题和 alt 标签中的关键字。
// Replaces both the website and general images path urls with character strings (used to prevent highlighting keywords found within the path urls)
if(strpos('http://my_domain_name.com/sports', $keywords) != false) {
$description = str_ireplace('http://my_domain_name.com/sports', '1q2w3e4r5t6y7u', $description);
}
if(strpos('http://my_domain_name.com/sports/images', $keywords) != false) {
$description = str_ireplace('http://my_domain_name.com/sports/images', '7u6y5t4r3e2w1q', $description);
}
// Highlights the Search Keywords
$description = str_ireplace($keywords, '<span style="color: #000000; background-color: #FFFF00; font-weight: normal;">'.$keywords.'</span>', $description);
// Replaces the character strings with the website and general images path urls
if(strpos('http://my_domain_name.com/sports', $keywords) != false) {
$description = str_ireplace('1q2w3e4r5t6y7u', 'http://my_domain_name.com/sports', $description);
}
if(strpos('http://my_domain_name.com/sports/images', $keywords) != false) {
$description = str_ireplace('7u6y5t4r3e2w1q', 'http://my_domain_name.com/sports/images', $description);
}
这是我
使用 PHP 的 DOMDocument
所能做的最好的事情。
$str = '<a href="http://my_domain_name.com/sports/info.php"><img class="icon" src="http://my_domain_name.com/sports/images/football.gif" title="Get the latest football sports news" alt="Get the latest football sports news" />Football sports news</a>';
$doc = new DOMDocument();
$fragment = $doc->createDocumentFragment();
$fragment->appendXML( $str);
$doc->appendChild( $fragment);
// Create the <span>
$node = $doc->createElement( 'span');
$node->setAttribute( 'style', 'color: #000000; background-color: #FFFF00; font-weight: normal;');
$node->nodeValue = 'sports';
foreach( $doc->getElementsByTagName( 'a') as $tag)
{
$img_tag = $tag->firstChild->cloneNode();
$text = $doc->createTextNode( $tag->textContent);
$tag->nodeValue = ''; // Clear out the contents of the <a>
// Get the text before and after the replacement
$start = strpos( $text->wholeText, 'sports');
$before = $text->substringData( 0, $start);
$after = $text->substringData( $start + strlen( 'sports'), strlen( $text->wholeText));
// Put the image tag back, along with the before text, the <span>, and the after text
$tag->appendChild( $img_tag);
$tag->appendChild( $doc->createTextNode( $before));
$tag->appendChild( $node);
$tag->appendChild( $doc->createTextNode( $after));
}
echo htmlentities( $doc->saveHTML()) . "'n";
这输出:
<a href="http://my_domain_name.com/sports/info.php">
<img class="icon" src="http://my_domain_name.com/sports/images/football.gif" title="Get the latest football sports news" alt="Get the latest football sports news">Football <span style="color: #000000; background-color: #FFFF00; font-weight: normal;">sports</span> news
</a>
演示
(你需要 PHP> 5.3)
只是使用字符串,我只想执行以下操作,因为所有属性值总是在元素值之前,很容易获得正确的匹配,然后只需使用回调将"sports"替换为您喜欢的任何内容。
可能更多你需要的:
function replacer($match)
{
global $replace_match_with_this, $string_to_replace;
return str_ireplace($string_to_replace, $replace_match_with_this, $match[0]);
}
$new_string = preg_replace_callback(sprintf('/>[^<>]*['s-]+%s['s-]+[^<>]*<'/a>/i', $keyword), 'replacer', $string, 1);
据推测,$keyword 和 $string_to_replace 保持相同的值,并且可以组合成一个变量。
xml_parse可用于去除HTML代码中的标记。http://www.w3schools.com/php/func_xml_parse.asp 是一个很好的教程,介绍如何使用它。
我会从我的字符串中删除所有 html 标签,然后使用:
str_replace($keyword, $replace_string, $string);
剩下的就做。
http://www.php.net/manual/en/function.str-replace.php
$replace_string = "<span fancy colours>{$keywords}</span>";
$string = '<a href="http://my_domain_name.com/sports/info.php"><img class="icon" src="http://my_domain_name.com/sports/images/football.gif" title="Get the latest football sports news" alt="Get the latest football sports news" />Football sports news</a>';
$exploded = explode("<", $string);
$tmp_array = array();
foreach ($exploded as $abit) {
$pos = (strpos($abit, ">") + 1); //get end of tag
$tmp_string = substr($abit, $pos);
if (strlen($tmp_string) > 1) { // has text outside of tags
$tmp_string = str_ireplace($keywords, $replace_string, $tmp_string);
$tmp_array[] = substr($abit,0,$pos) . $tmp_string;
} else {
$tmp_array[] = $abit;
}
}
$newstring = implode("<", $tmp_array);
echo $newstring;
现在可以有代表吗?