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、および画像タグの title と 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 段階の方法を使用していますが、タイトルや alt タグではなくURLに対してのみ機能します。また、タイトルと 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

すべての属性値は常に要素値の前に来るため、文字列を操作するだけで、適切な一致を簡単に取得できます。次に、コールバックを使用して「スポーツ」を好きなものに置き換えます。

おそらくもっと必要なもの:

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 は同じ値を保持し、1 つの変数に結合できます。

于 2012-06-12T23:51:40.493 に答える
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;

担当者はいますか?

于 2012-06-12T22:53:34.693 に答える