0

我正在针对文本正文运行一个文本字符串,以确定该文本字符串是否存在于特定上下文中(在本例中,在 h2 标题标签内)。我可以举个例子,它在我的应用程序的 localhost 安装上完美运行,在 Win 7 上使用 XAMPP 运行 PHP 5.3

但是,当我在一个实时 http 站点上测试同样的内容时,在 HostGator 上运行 PHP 5.3.8,我在同一个测试中得到了一个假阴性。

有任何想法吗?代码如下...

Keyword: Flirttipps für Männer

Content: <h2>Flirttipps für Männer</h2>


function doTheParse($heading='h2', $post) {
    //$content = $post->post_content;
    $content = '<h2>Flirttipps für Männer</h2>';
    $keyword = sanitize_string( trim(strtolower(getKeyword($post))) );
    $keyword = sanitize2($keyword);
    @$dom = new DOMDocument;
    @$dom->loadHTML(sanitize_string( strtolower($content) ));
    $xPath = new DOMXPath(@$dom);
    switch ($heading) {
        case "img-alt": 
            return $xPath->evaluate('boolean(//img[contains(@alt, "'.$keyword.'")])');
        default: 
            return $xPath->evaluate('boolean(/html/body//'.$heading.'[contains(.,"'.$keyword.'")])');
    }
}

function getKeyword($post) {
    global $spec;
    $myKeyword = get_post_meta($post->ID, 'my_keyword', true);
    if(isset($spec['keyword_default']) && $myKeyword == ""){$myKeyword = $post->post_title;}
    $myKeyword = htmlspecialchars_decode($myKeyword);
    return " ".$myKeyword;
}

function sanitize_string( $content ) {
    $regex = '/( [\x00-\x7F] | [\xC0-\xDF][\x80-\xBF] | [\xE0-\xEF][\x80-\xBF]{2} | [\xF0-\xF7][\x80-\xBF]{3} ) | ./x';
    return preg_replace($regex, '$1', $content);
}

function sanitize2($s) {
    $result = preg_replace("/[^\p{Latin}0-9&'-]+/u", " ", html_entity_decode($s, ENT_QUOTES));
    return $result;
}
4

1 回答 1

1

检查区域设置,例如 setLocale()。许多功能的工作方式因区域设置而异。

于 2011-12-14T19:33:03.903 回答