preg_match post-id - PullRequest
       3

preg_match post-id

2 голосов
/ 10 августа 2010

Я пытаюсь получить post-id моих сообщений.это формат, в котором он находится -> postid-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 Ответ

1 голос
/ 10 августа 2010

Вы можете попробовать использовать следующее регулярное выражение:

"/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 останавливается после того, как находит первое совпадение .

...