字符串是这样的 fhghfghdfpostm|a:2:{i:5;i:0;i:2;i:0;}dsfdsdfsd也可能是fhghfsdfsdfgdfgdfgdfgghdfpostm|a:2:{i:5;i:0;i:2;i:0;i:0;i:0;i:0;}dsfdsdfsdsdfsdfgsdgsdg我想要截取其中的 postm|a:2:{i:5;i:0;i:2;i:0;}或者postm|a:2:{i:5;i:0;i:2;i:0;i:0;i:0;i:0;} 但是这个字符串的长度不固定 求代码

解决方案 »

  1.   

    本帖最后由 xuzuning 于 2012-06-04 12:06:54 编辑
      

  2.   

    [User:root Time:13:27:08 Path:/home/liangdong/php]$ php preg.php 
    Array
    (
        [0] => postm|a:2:{i:5;i:0;i:2;i:0;}
        [1] => postm|a:2:{i:5;i:0;i:2;i:0;i:0;i:0;i:0;}
    )
    [User:root Time:13:27:10 Path:/home/liangdong/php]$ cat preg.php 
    <?php
    $str = <<<EOF
    fhghfghdfpostm|a:2:{i:5;i:0;i:2;i:0;}dsfdsdfsd
    fhghfsdfsdfgdfgdfgdfgghdfpostm|a:2:{i:5;i:0;i:2;i:0;i:0;i:0;i:0;}dsfdsdfsdsdfsdfgsdgsdg
    EOF;$nmatches = preg_match_all('/postm.*}/isU', $str, $matches);
    if ($nmatches) {
            print_r($matches[0]);
    }?>