本文介绍了使用 xPath 还是正则表达式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的两种方法都有相同的目的:扫描帖子的内容并确定是否至少有一个 img 标签具有包含正在测试的关键字"的 alt 属性.

The two methods below each serve the same purpose: scan the content of the post and determine if at least one img tag has an alt attribute which contains the "keyword" which is being tested for.

我是 xPath 的新手,更喜欢使用它,具体取决于与正则表达式版本相比该方法的成本...

I'm new to xPath and would prefer to use it depending on how expensive that approach is compared to the regex version...

方法 #1 使用 preg_match

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);
        }

方法 #2 使用 xPath

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.'")]])');
}

推荐答案

如果您正在解析 XML,您应该使用 XPath,因为它正是为此目的而设计的.XML/XHTML 不是正则语言,不能被正则表达式正确解析.您也许可以编写一个在某些时候有效的正则表达式,但在某些特殊情况下它会失败.

If you are parsing XML you should use XPath as it was designed exactly for this purpose. XML / XHTML is not a regular language and cannot be parsed correctly by regular expressions. You may be able to write a regular expression which works some of the time but there will be special cases where it will fail.

这篇关于使用 xPath 还是正则表达式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 22:17