douchen4915 2013-07-11 07:47
浏览 52
已采纳

PHP正则表达式帮助preg_match

http://regexr.com?35hk2

The above site shows the correct regex but when I do it with PHP it doesn't show up certain names such as 'JJ5x5's White Top Hat'

Here is the PHP :

<?php
    function newEcho($Value){
        echo $Value . "<br>";
    };
    function cURLAuto($URL){
        $Channel = curl_init();
        curl_setopt($Channel, CURLOPT_URL, $URL);
        curl_setopt($Channel, CURLOPT_RETURNTRANSFER, 1);
        return curl_exec($Channel);
    };
    function autoMatchAll($String,$Pattern){
        $Found = array();
        $Match = preg_match_all($Pattern,$String,$Found);
        return $Found;
    };
    function replaceMatch($String,$Pattern,$Subject){
        return str_replace($Pattern,$Subject,$String);
    };
    $Count = 0;
    $Output = cURLAuto("www.roblox.com/catalog/json?Subcategory=2&SortType=0&SortAggregation=3&SortCurrency=0&LegendExpanded=true&Category=2&PageNumber=1");
    $AssetId = autoMatchAll($Output,'/"AssetId":[\d]+/');
    $Name = autoMatchAll($Output,'/"Name":"[\w\s\d\-' . "\'" . ']+"/');
    foreach($AssetId[0] as $Value){
        newEcho(replaceMatch($Value,'"AssetId":',"") . ":" . replaceMatch(replaceMatch($Name[0][$Count],'"Name":"',""),'"',""));
        $Count++;
    };
    echo $Output
?>

$Name is where I am having problems with the regex cause it is showing only some of the names when displaying the running the code. The regex for the $Name is

/"Name":"[\w\s\d\-\']+"/

But due to the fact I cannot use ' or " as the string I had to make it

'/"Name":"[\w\s\d\-' . "\'" . "]+/"

But could you help me with this as I would like to fix this.

  • 写回答

2条回答 默认 最新

  • duangang1991 2013-07-11 08:02
    关注

    My bet is that the ' in JJ5x5's White Top Hat is a "typographic apostrophe", (Unicode: U+2019 "RIGHT SINGLE QUOTATION MARK", Windows codepage 1252: 0x92, UTF-8 in PHP: "\xE2\x80\x99"). To tell the typographic apostrophe/quote from the ASCII single quote: if it points straight down (in the original string!), it's an ASCII single quote, if it doesn't, it's a typographic apostrophe/quote.

    If you simply want to match anything up to the closing double quotes, use '/"Name":"[^"]+"/', unless you can have escaped double quotes in the name, in which case the regex becomes (in PHP) '/"Name":"(?:[^\\\\"]|\\\\[\\\\"])+"/' (add other possible escapes to the last class).

    BTW, you don't need to split the string of the regex into differently delimited strings (all you have to do is escape the current delimiter), and, if you do, you don't need to escape the single quote in a string delimited by double quotes.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?