在字符串

时间:2015-12-15 13:12:10

标签: php string

我想检查一下这个字符串是否正确:

  

“title”:“纽约市”

位于此巨大字符串中:

  

{“response”:[{“cid”:378,“title”:“纽约市”,“地区”:“纽约县”,“地区”:“纽约”},{“cid” :2211916,“title”:“West New York”,“area”:“Hudson County”,“region”:“New Jersey”},{“cid”:4907470,“title”:“New York”,“area” “:”考德威尔县“,”地区“:”密苏里“},{”cid“:4910556,”标题“:”小纽约“,”地区“:”马歇尔县“,”地区“:”阿拉巴马州“} ,{“cid”:4923978,“title”:“New York”,“area”:“Santa Rosa County”,“region”:“Florida”},{“cid”:4944119,“title”:“New York “,”区域“:”巴拉德县“,”地区“:”肯塔基州“},{”cid“:4963231,”标题“:”纽约乡镇“,”地区“:”考德威尔县“,”地区“ :“Missouri”},{“cid”:5004855,“title”:“Little New York”,“area”:“Gonzales County”,“region”:“Texas”},{“cid”:5005637,“title “:”纽约“,”地区“:”亨德森县“,”地区“:”得克萨斯“},{”cid“:5022669,”标题“:”纽约“,”地区“:”韦恩县“, “地区”:“爱荷华州”},{“cid”:5046761,“title”:“纽约米尔斯”,“地区”:“水獭尾巴县”,“地区”:“明尼苏达州”},{“cid”: 5052155,“title”:“纽约乡镇”,“地区”:“约克县”,“地区”: “Nebraska”},{“cid”:5056268,“title”:“西纽约镇”,“地区”:“哈德逊县”,“地区”:“新泽西”},{“cid”:5058154, “标题”:“东纽约”,“地区”:“国王郡”,“地区”:“纽约”},{“cid”:5060715,“标题”:“纽约米尔斯”,“地区”: “奥奈达县”,“地区”:“纽约”},{“cid”:5098755,“标题”:“纽约”,“地区”:“Cibola县”,“地区”:“新墨西哥”}, {“cid”:5109737,“title”:“纽约矿区”,“地区”:“埃尔科县”,“地区”:“内华达”}}}

我尝试使用 strpos(),但我得到了所有这些结果。

编辑: 这是代码:

 $question = "\"title\"" . ":" . "\"$city\"";

 if(preg_match("~\b$question\b~", $cities)) {
    flag = 1;
}

其中 cities 上面是 huge 字符串,而 city 是纽约市。

1 个答案:

答案 0 :(得分:2)

您的巨大字符串实际上是一个json字符串,您可以使用json_decode()函数进行解析。

参考:

我相信您正在尝试在json字符串的title属性中搜索纽约市,您可以这样做:

// here $json is your json string
$json_array = json_decode($json, true);
$string_to_search = "New York City";

$found = false;
for($i = 0; $i < count($json_array['response']); ++$i){
    if($json_array['response'][$i]['title'] == $string_to_search){
        $found = true;
        break;
    }
}

if($found){
    // string exists
}else{
    // string doesn't exists
}
相关问题