ハイパーリンク 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);
}