1

如何在不替换超链接 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 (sports) 替换为:

<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>

提前致谢。

编辑 - 附加信息

目前我正在使用以下两步方法,它仅适用于 URLs而不适用于 title 和 alt 标签。我也不需要替换 title 和 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);
   }
4

3 回答 3

2

这是我使用 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)

于 2012-06-12T23:33:08.550 回答
0

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;

现在能有rep吗?

于 2012-06-12T22:53:34.693 回答
0

只需使用字符串,我只需执行以下操作,因为所有属性值总是在元素值之前很容易获得正确的匹配,然后只需使用回调将“运动”替换为您喜欢的任何内容。

可能更多你需要的东西:

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 持有相同的值,可以组合成一个变量。

于 2012-06-12T23:51:40.493 回答