[英]How to find the second string between two strings
I have been working on a script that pulls information from a certain website. 我一直在研究从某个网站提取信息的脚本。 The said website pulls the information from a database and displays it in a way the user can easily read it (like always). 所述网站从数据库中提取信息并以用户可以容易地读取它的方式显示它(像往常一样)。
Imagine it looks like this: 想象一下它看起来像这样:
Var1 : result1 Var2: result2 Var3: result3 Var1 :result1 Var2: result2 Var3: result3
What my script does is that it reads the page's source code and retrieves "result1", "result2" and "result3" by obtaining the text between two strings. 我的脚本所做的是它读取页面的源代码并通过获取两个字符串之间的文本来检索“result1”,“result2”和“result3”。
Sample code: 示例代码:
<?php
function get_string_between($string, $start, $end) {
$string = " ".$string;
$ini = strpos($string,$start);
if ($ini == 0) return "";
$ini += strlen($start);
$len = strpos($string,$end,$ini) - $ini;
return substr($string,$ini,$len);
}
function check($url) {
// usually, $fullstring = file_get_contents($url);
$fullstring = "<string1>result1</string1><string1>result2</string1><string1>result3</string1>";
$result = get_string_between($fullstring, "<string1>", "</string1>");
echo "<b>Result: </b>".$result;
}
check("random"); // just to execute the function
?>
In case you wonder why I have the check() function there it is because this code is part of something bigger and I need a solution that works in this case scenario, so I tried to keep it immaculate. 如果你想知道为什么我有check()函数,那是因为这个代码是更大的东西的一部分,我需要一个在这种情况下工作的解决方案,所以我试着保持完美无暇。
Now, I can easily get "result1" because it's the first occurrence, but how can I get "result2" and "result3"? 现在,我可以轻松获得“result1”,因为它是第一次出现,但我怎样才能获得“result2”和“result3”?
Thank you :) 谢谢 :)
Use a regex to extract all of the matches, then pick the ones you want: 使用正则表达式提取所有匹配项,然后选择您想要的匹配项:
function get_string_between($string, $start, $end)
{
preg_match_all( '/' . preg_quote( $start, '/') . '(.*?)' . preg_quote( $end, '/') . '/', $string, $matches);
return $matches[1];
}
The regex will capture anything between the $start
and $end
variables. 正则表达式将捕获$start
和$end
变量之间的任何内容。
Now the function returns an array of all of the result values, which you can pick which one you want: 现在该函数返回一个包含所有结果值的数组,您可以选择所需的结果值:
list( $first, $second, $third) = get_string_between( $string, "<string1>", "</string1>");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.