Php 通过maches文本从文件中提取信息
我有一个php文件,在这个文件中我有一些数据Php 通过maches文本从文件中提取信息,php,regex,Php,Regex,我有一个php文件,在这个文件中我有一些数据 <This is the content of the text file> "dim://realthings1/true" "rel://stop1" "dim://realthings2/false" "rel://stop2" "dim://realthings3/false" "rel://stop3" "dim://realthings4/false" "rel://stop4" <This is the rest
<This is the content of the text file>
"dim://realthings1/true"
"rel://stop1"
"dim://realthings2/false"
"rel://stop2"
"dim://realthings3/false"
"rel://stop3"
"dim://realthings4/false"
"rel://stop4"
<This is the rest of content>
这段代码可以很好地处理这些数据
dim:“房地产1”
我试图更改此代码,但没有结果!
请帮忙 如果你只想要那一行,你可以使用非贪婪标志
U
$Text=file_get_contents("test.php");
$Match=array();
if (preg_match('/dim\s*:\s*([^\']+)[\'"]/U',$Text,$Match)){
echo $Match[1];
}
所有比赛
$Text=file_get_contents("test.php");
$Match=array();
if (preg_match_all('/dim\s*:\s*([^\']+)[\'"]/U',$Text,$Match)){
print_r($Match[1]);
}
以下内容将匹配以dim
开头的任何内容,然后是0个或更多空格:然后是0个或更多空格任何内容(不是“或”或“或”空格),然后是“或”或“或”空格。不要贪心。括号内匹配的项目是您在$Matches[1]
中得到的。希望现在一切都清楚了
/dim\s*:\s*([^\'"\s]+)[\'"\s]/U
这可能是您想要的
preg_match('/dim\s*:\s*([^\']+)/,$Text,$match)
可以,但回显不是它在“dim://realthings1/true”之后的所有回显rel://stop1""dim://realthings2/false""rel://stop2""dim://realthings3/false""rel://stop3""dim://realthings4/false""rel://stop4“
我只需要//realthings1/true
这没关系,但如何只回显de秒dim://if 如果使用preg\U match\U all
而不是preg\U match
,则$Matches[1]
将是所有匹配项的和数组。我不能在('/dim\s*:\s*([^\']+)
[\'“]/U',$Text,$match)中添加一个“空格”分隔符吗).假设我有”dim://transfer/true '这是空格'重试“
,但我只需要echo//transfer/true
确定您可以/dim\s*:\s*([^\']+)[\'”\s]/U
但是dim://real things1/true
将匹配为//real
注意real
和things1
之间的空格
/dim\s*:\s*([^\'"\s]+)[\'"\s]/U