特殊字符解析(ü,ä等)。适用于localhost,而不是http

时间:2011-12-14 19:26:17

标签: php regex htmlspecialchars

我正在对文本正文运行一串文本,以确定文本字符串是否存在于特定上下文中(在本例中,在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;
}

1 个答案:

答案 0 :(得分:1)

检查区域设置,例如的setLocale()。根据区域设置,许多函数的工作方式不同。

相关问题