Php 查询类字符串参数regex
从如下文本中:Php 查询类字符串参数regex,php,regex,Php,Regex,从如下文本中: category=[123,456,789], subcategories, id=579, not_in_category=[111,333] 我需要一个正则表达式来获得如下内容: $params[category][0] = 123; $params[category][1] = 456; $params[category][2] = 789; $params[subcategories] = ; // I just need to know that this exist
category=[123,456,789], subcategories, id=579, not_in_category=[111,333]
我需要一个正则表达式来获得如下内容:
$params[category][0] = 123;
$params[category][1] = 456;
$params[category][2] = 789;
$params[subcategories] = ; // I just need to know that this exists
$params[id] = 579;
$params[not_category][0] = 111;
$params[not_category][1] = 333;
谢谢大家的帮助
PS
正如你所建议的,我澄清,项目的结构和数量可能会发生变化。
基本上,结构是:
key=value, key=value, key=value, ...
其中值可以是:
- 单个值(例如
或category=123
或postID=123
,…)mykey=myvalue
- “数组”(例如
)类别=[123456789]
- 一种“布尔值”,其中的真值是基于数组中存在“键”这一事实的假设(例如
)子类别
我想这会让你把火柴拿出来。。。实际上没有测试它,但这可能是一个很好的起点
然后只需将匹配项推送到所需数组中的正确位置。还要测试子类别
字符串是否与strcmp
或其他内容一起存在
另外,请注意,我假设您的
主题字符串具有固定的数据类型结构。。。如果它经常变化,您需要的远不止此…此方法应该足够灵活:
$str = 'category=[123,456,789], subcategories, id=579, not_in_category=[111,333]';
$str = preg_replace('#,([^0-9 ])#',', $1',$str); //fix for string format with no spaces (count=10,paginate,body_length=300)
preg_match_all('#(.+?)(,[^0-9]|$)#',$str,$sections); //get each section
$params = array();
foreach($sections[1] as $param)
{
list($key,$val) = explode('=',$param); //Put either side of the "=" into variables $key and $val
if(!is_null($val) && preg_match('#\[([0-9,]+)\]#',$val,$match)>0)
{
$val = explode(',',$match[1]); //turn the comma separated numbers into an array
}
$params[$key] = is_null($val) ? '' : $val;//Use blank string instead of NULL
}
echo '<pre>'.print_r($params,true).'</pre>';
var_dump(isset($params['subcategories']));
备选(处理前无字符串操作):
$str='count=10,paginate,body_length=300,rawr=[1,2,3];
preg#u match#u all('#(.+?)(,([^0-9,])|$)#',$str,$sections)//获取每个部分
$params=array();
foreach($k=>$param的[1]部分)
{
list($key,$val)=分解('=',$param);//将“=”的任一侧放入变量$key和$val中
$key=isset($sections[3][$k-1])?trim($sections[3][$k-1])。$key:$key;//获取上一次匹配窃取的第一个字符
如果(!is_null($val)&&preg_match('\\[([0-9,]+)\]\]\\\\]\\\\\\\',$val,$match)>0)
{
$val=explode(“,”,$match[1]);//将逗号分隔的数字转换为数组
}
$params[$key]=is_null($val)?“”:$val;//使用空字符串而不是null
}
回显“”。打印($params,true)。“”;
另一种替代方法:为安全起见,在处理之前完全重新格式化字符串
Array
(
[category] => Array
(
[0] => 123
[1] => 456
[2] => 789
)
[subcategories] =>
[id] => 579
[not_in_category] => Array
(
[0] => 111
[1] => 333
)
)
$str='count=10,paginate,body_length=300,rawr=[1,2,3],name=mike';
$str=preg_replace(数组('.\s+#','#,([^0-9])#'),数组(''.',$1'),$str)//修正了不同的字符串格式
preg#u match#u all('#(.+?)(,[^0-9]|$)#',$str,$sections)//获取每个部分
$params=array();
foreach($param为第[1]节)
{
list($key,$val)=分解('=',$param);//将“=”的任一侧放入变量$key和$val中
如果(!is_null($val)&&preg_match('\\[([0-9,]+)\]\]\\\\]\\\\\\\',$val,$match)>0)
{
$val=explode(“,”,$match[1]);//将逗号分隔的数字转换为数组
}
$params[$key]=is_null($val)?“”:$val;//使用空字符串而不是null
}
回显“”。打印($params,true)。“”;
输出:
你从哪里得到这个文本的?它是模板引擎的params块:{block:Posts category=[123456789],subcategories,id=579,not_in_category=[111333]}你到目前为止做了哪些尝试?@AldoB你需要$params
数组,还是只需要知道subcategories
是否在列表中,但是似乎什么都不管用。为了得到你需要的答案,你应该试着解释一下结构中的变化。例如,如果类别
数组中项目的#不同;如果中的项目的#不在_类别
中变化;等等…当我刷新页面时,我自己正在测试explode
。。。你早到了!:)GJ!此解决方案不适用于这样的文本:count=10,paginate,body_length=300
可能是因为param key附近有逗号。@AldoB您可以运行$str=preg_replace(“#,([^0-9])#,“,$1,$str代码>在进程之前,在commas@AldoB如果您不想在运行进程之前更改字符串,我还有另一个修复方法。如果你愿意,请告诉我it@SmokeyPHP我试图使用您上次的更新,但是对于像$str='count=10,paginate,body_length=300,rawr=[1,2,3],name=mike'代码>结果不符合预期:数组([count]=>10[paginate]=>[body\u length]=>300[rawr]=>[1[2,3]=>[name]=>mike)
我正在尝试编辑代码,从您的代码开始会更容易。。谢谢
Array
(
[category] => Array
(
[0] => 123
[1] => 456
[2] => 789
)
[subcategories] =>
[id] => 579
[not_in_category] => Array
(
[0] => 111
[1] => 333
)
)
bool(true)
$str = 'count=10,paginate,body_length=300,rawr=[1,2,3]';
preg_match_all('#(.+?)(,([^0-9,])|$)#',$str,$sections); //get each section
$params = array();
foreach($sections[1] as $k => $param)
{
list($key,$val) = explode('=',$param); //Put either side of the "=" into variables $key and $val
$key = isset($sections[3][$k-1]) ? trim($sections[3][$k-1]).$key : $key; //Fetch first character stolen by previous match
if(!is_null($val) && preg_match('#\[([0-9,]+)\]#',$val,$match)>0)
{
$val = explode(',',$match[1]); //turn the comma separated numbers into an array
}
$params[$key] = is_null($val) ? '' : $val;//Use blank string instead of NULL
}
echo '<pre>'.print_r($params,true).'</pre>';
$str = 'count=10,paginate,body_length=300,rawr=[1, 2,3] , name = mike';
$str = preg_replace(array('#\s+#','#,([^0-9 ])#'),array('',', $1'),$str); //fix for varying string formats
preg_match_all('#(.+?)(,[^0-9]|$)#',$str,$sections); //get each section
$params = array();
foreach($sections[1] as $param)
{
list($key,$val) = explode('=',$param); //Put either side of the "=" into variables $key and $val
if(!is_null($val) && preg_match('#\[([0-9,]+)\]#',$val,$match)>0)
{
$val = explode(',',$match[1]); //turn the comma separated numbers into an array
}
$params[$key] = is_null($val) ? '' : $val;//Use blank string instead of NULL
}
echo '<pre>'.print_r($params,true).'</pre>';
$str = 'category=[123,456,789], subcategories, id=579, not_in_category=[111,333]';
$main_arr = preg_split('/(,\s)+/', $str);
$params = array();
foreach( $main_arr as $value) {
$pos = strpos($value, '=');
if($pos === false) {
$params[$value] = null;
} else {
$index_part = substr($value, 0, $pos);
$value_part = substr($value, $pos+1, strlen($value));
$match = preg_match('/\[(.*?)\]/', $value_part,$xarr);
if($match) {
$inner_arr = preg_split('/(,)+/', $xarr[1]);
foreach($inner_arr as $v) {
$params[$index_part][] = $v;
}
} else {
$params[$index_part] = $value_part;
}
}
}
print_r( $params );
Array
(
[category] => Array
(
[0] => 123
[1] => 456
[2] => 789
)
[subcategories] =>
[id] => 579
[not_in_category] => Array
(
[0] => 111
[1] => 333
)
)