用PHP提取和解析一个大字符串
我想使用php从字符串中提取标签和数据。字符串如下所示:用PHP提取和解析一个大字符串,php,string,parsing,Php,String,Parsing,我想使用php从字符串中提取标签和数据。字符串如下所示: Label: Info; Label1: Info1; Label2: Info2; Label3: Info3; .............. 我的解决方案是对每个标签使用strpos,将其保留在变量中,然后提取太慢的标签的数据。你能给我推荐另一种方法吗?我肯定不是正则表达式专家,但这是我提出的有效解决方案 $ptn = "/(Label[0-9]?):?.?(.+);/"; $str = "Label: Info; Lab
Label: Info;
Label1: Info1;
Label2: Info2;
Label3: Info3;
..............
我的解决方案是对每个标签使用strpos,将其保留在变量中,然后提取太慢的标签的数据。你能给我推荐另一种方法吗?我肯定不是正则表达式专家,但这是我提出的有效解决方案
$ptn = "/(Label[0-9]?):?.?(.+);/";
$str = "Label: Info;
Label1: Info1;
Label2: Info2;
Label3: Info3;";
preg_match_all($ptn, $str, $matches, PREG_SET_ORDER);
现在在$matches
上使用print\r
返回以下内容:
Array
(
[0] => Array
(
[0] => Label: Info;
[1] => Label
[2] => Info
)
[1] => Array
(
[0] => Label1: Info1;
[1] => Label1
[2] => Info1
)
[2] => Array
(
[0] => Label2: Info2;
[1] => Label2
[2] => Info2
)
[3] => Array
(
[0] => Label3: Info3;
[1] => Label3
[2] => Info3
)
)
你的密码在哪里?我们无法解释为什么速度太慢。您需要提取所有标签及其数据,还是只查找某些特定标签?@SilverSnake查找某些特定标签,以提取数据。