Php 每次分解url的最后一部分
在我的网站上,我有一些类似这样的URLPhp 每次分解url的最后一部分,php,Php,在我的网站上,我有一些类似这样的URL http://example.com/our-post-name-here-number-one-81347.html our post name number one http://example.com/our-post-name-here-number-two-81348.html our post name number two http://example.com/our-post-name-here-number-three-81349.ht
http://example.com/our-post-name-here-number-one-81347.html our post name number one
http://example.com/our-post-name-here-number-two-81348.html our post name number two
http://example.com/our-post-name-here-number-three-81349.html our post name number three
我需要在包含一个随机数的.html之前得到正确的部分,在任何情况下,这个数都不会相同
目前,我将这些URL存储在一个.txt文件中,该文件还包含文章的标题,并通过下面的方法进行分解
$array = explode("\n", file_get_contents('http://example.com/urls/delete.txt'));
foreach ($array as $item) {
$removed = "copyright";
$nothing = "";
$url = explode(" ", $item);
$data = $url[0];
$explode = explode(".html", $data);
// other functions
$primary = $explode[0];
print $primary;
print '<br>';
}
当我只希望它返回这个81347 81348 81349时。如果保证相同的格式,您可以按-,再拆分一次,并使用以下内容获取最后一个元素: 我会用正则表达式
$array = explode("\n", file_get_contents('http://example.com/urls/delete.txt'));
foreach ($array as $item)
{
$regex = "/.*-([\d]+)\.html.*$/";
preg_match ( $regex , $item , $matches );
$primary = $matches[1];
print $primary;
print '<br>';
}
将其传递给此函数:
function url_post_id($url) {
$i = strripos($url, "-");
$j = stripos($url, ".", $i);
return substr($url, $i+1, $j-$i-1);
}
$array = explode("\n", file_get_contents('http://example.com/urls/delete.txt'));
foreach ($array as $item)
{
$regex = "/.*-([\d]+)\.html.*$/";
preg_match ( $regex , $item , $matches );
$primary = $matches[1];
print $primary;
print '<br>';
}
function url_post_id($url) {
$i = strripos($url, "-");
$j = stripos($url, ".", $i);
return substr($url, $i+1, $j-$i-1);
}