Use xPath or Regex?

Both of the methods below serve the same purpose: scanning the contents of a message and determining whether at least one img tag has an alt attribute that contains the “keyword” that is being tested.

I am new to xPath and prefer to use it depending on how expensive this approach compares with the regex version ...

Method # 1 uses 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);
        }

Method # 2 uses 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.'")]])');
}
+5
source share
2 answers

XML, XPath, . XML/XHTML . , , , , .

+14

RegEx XML , , .

, , .

, XPath 2.0 RegEx, RegEx XPath. , , , XPath 2.0

+4

All Articles