2010-08-09 4 views
2

내 게시물 ID를 가져 오려고합니다. 이것은 형식입니다. -> postid-785645 숫자가 무작위로 생성됩니다.preg_match post-id

<?php 
$mysite = "http://example.com"; 
$wholepage = @file_get_contents($mysite); 

// I want to use preg_match to echo just the numbers. out of the whole page. 
// Basically i'm trying to have is search the whole page for -> postid-(randomly generated number) and have it echo the number from things that are in this format. 

?> 

답변

1

다음과 같은 정규 표현식을 사용하여 시도 할 수 :

"/postid-[0-9]+/" 

예제 코드 :

$wholepage = 'foo postid-785645 bar postid-785646 baz'; 
$matches = array(); 
$string = preg_match_all("/postid-[0-9]+/", $wholepage, $matches); 
print_r($matches); 

결과 :

 
Array 
(
    [0] => Array 
     (
      [0] => postid-785645 
      [1] => postid-785646 
     ) 

) 

내가 preg_match_all를 사용하는 대신 0거야 preg_match이 첫 번째 일치 항목을 찾은 후 중지하기 때문에이 표시됩니다.

+0

감사합니다 ... 사람 .. 좋은 작동합니다. – ashoopatel