प्रत्येक के नीचे दो विधियां एक ही उद्देश्य प्रदान करती हैं: पोस्ट की सामग्री को स्कैन करें और निर्धारित करें कि कम से कम एक आईएमजी टैग में एक alt विशेषता है जिसमें "कीवर्ड" है जिसका परीक्षण किया जा रहा है।एक्सपैथ या रेगेक्स का प्रयोग करें?
मैं xPath के लिए नया हूँ और यह कैसे महंगा है कि दृष्टिकोण regex संस्करण की तुलना में है पर निर्भर करता है का उपयोग करना पसंद करेंगे ...
विधि # 1 preg_match का उपयोग करता
function image_alt_text_has_keyword($post)
{
$theKeyword = trim(wpe_getKeyword($post));
$theContent = $post->post_content;
$myArrayVar = array();
preg_match_all('/<img\s[^>]*alt=\"([^\"]*)\"[^>]*>/siU',$theContent,$myArrayVar);
foreach ($myArrayVar[1] as $theValue)
{
if (keyword_in_content($theKeyword,$theValue)) return true;
}
return false;
}
function keyword_in_content($theKeyword, $theContent)
{
return preg_match('/\b' . $theKeyword . '\b/i', $theContent);
}
विधि # 2 का उपयोग करता है xPath
function keyword_in_img_alt()
{
global $post;
$keyword = trim(strtolower(wpe_getKeyword($post)));
$dom = new DOMDocument;
$dom->loadHTML(strtolower($post->post_content));
$xPath = new DOMXPath($dom);
return $xPath->evaluate('count(//a[.//img[contains(@alt, "'.$keyword.'")]])');
}
"constains" का उपयोग करना सबसे अच्छा है? मुझे लगता है कि आपके पास एक टाइपो है। –
धन्यवाद, टाइपो को ठीक किया गया और xPath –