1

如果我有这样"location":"Denton TX"的字符串(包括双引号),我只想得到Denton TX. 我怎样才能为那个东西写正则表达式?

我试过了

function getInfo($info,$content){
    echo 'getting INFO';
    preg_match_all("\.".$info."\.:\"[^\"]*(.*?)\.,",$content,$matches,PREG_PATTERN_ORDER);
    print_r($matches);
    return $matches[0];
}

我把“位置”放进去$info,但它不起作用。

4

3 回答 3

0

I'd go with this:

"\"".$info."\":\"([^\"]+)\""

I'm not sure why you wrote this particular bit, but the main problem in your regex is the [^\"]*, which is too greedy and will consume everything, and leave (.*?) empty (well, unless is starts with a double quote, I guess). I also don't really understand why you what a comma in the end, but maybe your example wasn't descrptive... In any case, the regex I provided should match your example, provided $info contains the string location.

于 2013-04-01T19:47:21.703 回答
0

简单地获取引号之间的每个字符。你离得太近了。

"\.".$info."\.:\"([^\"]*)\"
于 2013-04-01T19:39:03.240 回答
0

试试这个: (?<=:")(.*)(?=")

我使用类似的方法从 Android 应用程序中的 XML 响应中删除元素标签。祝你好运

于 2013-04-01T20:03:33.577 回答