php
  • regex
  • 2011-08-26 90 views 0 likes 
    0

    我在包含度符號的字符串上preg_match_all時遇到問題。代碼示例如下。遇到preg_match_all和度符號問題

    //Sample data 
    $x = "<array_0> 
         <id>text-21650</id> 
         <text>Lat/Long 38° 57' 34 N, 106° 21' 38 W</text> 
         </array_0>"; 
    
    $reels = '/<(\w+)\s*([^\/>]*)\s*(?:\/>|>(.*)<\/\s*\\1\s*>)/s'; 
    
    preg_match_all($reels, $x, $elements); 
    
    foreach ($elements[1] as $ie => $xx) { 
        $name = $elements[1][$ie]; 
        $cdend = strpos($elements[3][$ie], "<"); 
        if ($cdend > 0) { 
        $xmlary[$name] = substr($elements[3][$ie], 0, $cdend - 1); 
        } 
    
        if (preg_match($reels, $elements[3][$ie])) 
        $xmlary[$name] = processEl($elements[3][$ie]); 
        else if ($elements[3][$ie] !== null) { 
        $xmlary[$name] = $elements[3][$ie]; 
        } 
    } 
    

    由於某種原因,它無法正常使用度數符號。如果我把它拿出來就行了。我真的很想找到一種方式,讓他們不用改變就能留在那裏。我也想知道是否可能有其他可能導致問題的延伸角色。

    任何幫助將不勝感激。 謝謝

    +2

    「出於某種原因,它不能正常工作」 - >這是什麼意思?預期的行爲和實際產出是什麼? – JRL

    +2

    免費的建議...使用XML解析器,而不是用於解析XML的正則表達式。 – sberry

    +0

    [preg_match座標與度符號]可能的重複(http://stackoverflow.com/questions/5355874/preg-match-coordinates-with-degree-sign) –

    回答

    3

    看看this previous answer on StackOverflow。基本上,你將不得不切換到Unicode匹配。

    改爲使用mb_ereg_match來支持UTF-8字符。文檔: http://php.net/manual/en/book.mbstring.php

    初始化MB *是這樣的:

    mb_regex_encoding( 'UTF-8'); mb_internal_encoding( 'UTF-8');

    3

    我有同樣的問題,this other post from stackoverflow幫助了我。基本上,爲了尋找學位符號,你會使用\ x {00B0},即。

    preg_match_all(「/ \ xBB} /」,$ x,$ elements);

    相關問題