PHP 在 'strstr' 匹配后获取 5 个字符


PHP get the 5 characters after the 'strstr' match

变量$page包含一个网页(完整的HTML文件),但我想在以下行过滤该文件:

<a href="http://[website]/[folder]/

我希望在字符串中解析后的 5 个字符。

但是这个字符串在$page里面是多次的,所以数字也必须存储在一个数组中。

因此,如果找到与<a href="http://[website]/[folder]/23455匹配项,如何使"23455"进入$nums[0]

如果找到另一个与<a href="http://[website]/[folder]/12345匹配项,则"12345"将被放入$nums[1]

看看这个正则表达式 http://net.tutsplus.com/tutorials/other/8-regular-expressions-you-should-know/可能适合你:

/^(https?:'/'/)?(['da-z'.-]+)'.([a-z'.]{2,6})(['/'w '.-]*)*'/?$/ 

根据我从您的问题中了解到的情况,您只是想获取 url 的结尾部分,然后进行比较然后将值存储在数组中?我相信这段代码应该可以解决问题。

$nums = new array(); //declare your array
$a = new SimpleXMLElement('<a href=<a href="http://[website]/[folder]/23455">Your Link</a>'); //create a new simple xml element
$a = $a['href'];
$array = explode("/",$a); //split the string into an array by using the / as the delimiter
if($array[count($array)-1] == '23455') { //get the count of the array and -1 from the count to get the last index then compare to required number
$nums[0] = '23455'; //store value in array
} else if ($array[count($array)-1] == '12345'{
$nums[1] = '12345'; //
}