preg_match post-id

时间:2010-08-09 21:37:47

标签: php preg-match

我正在努力获得post-id我的帖子。 这是它的格式 - >帖子ID-785645 这些数字是随机生成的。

<?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 个答案:

答案 0 :(得分: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代替preg_match,因为preg_match在找到第一个匹配后停止。