2017-10-08 3 views
2

내 웹 사이트에 이런 모양의 URL이 있습니다.매번 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.html our post name number three 

그리고이 숫자는 지금까지 동일합니다없는 입장에서, 임의의 숫자를 포함 .html을 befor 부품 권리를 얻을 필요가 있어요.

현재이 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>'; 
} 

위의 코드는 현재이를 반환하고 있습니다.

http://example.com/our-post-name-here-number-one-81347 
http://example.com/our-post-name-here-number-two-81348 
http://example.com/our-post-name-here-number-three-81349 

나는 만이 81347 81348 81349을 반환 할 때. 같은 형식이 보장되면

답변

3

내가 정규식을 사용합니다. 이 함수

$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>'; 
} 
0

, 당신은 -에 의해 한 번 더 시간을 분할하고, array_pop()와 마지막 요소를 얻을 수 있습니다 :

$pieces = explode("-", $primary); 
$number = array_pop($pieces); 
print $number; 

Demo

1

패스를 :

function url_post_id($url) { 
    $i = strripos($url, "-"); 
    $j = stripos($url, ".", $i); 
    return substr($url, $i+1, $j-$i-1); 
} 
관련 문제