简单的preg_match需要帮助

时间:2015-06-26 19:18:54

标签: php preg-match

我是php的新手,需要别人的帮助。从下面的代码中,我想要“我是一个好孩子”这一行,但它打印出“我是一个好男孩::没有::她是一个好女孩”

$data = "None::I am a good boy::None::She is a good girl::None::";

$pattern = "/None::(.*)::None::/";
preg_match($pattern, $data, $results);
echo $results[1];

而在另一个,我想要“2015-06-02 10:13:54”,但我得到“015-06-02 10:13:54”

$data2 = "::2015-06-02 10:13:54::None::";

$pattern2 = "/::\d(.*)::None::/";
preg_match($pattern2, $data2, $results);
echo $results[1];

2 个答案:

答案 0 :(得分:2)

preg_match中的位变化: -

    <?php  
       $data = "None::I am a good boy::None::She is a good girl::None::";

       $pattern = "/None::(.*?)::None::/"; // ? is added to capture anything present between None:: and  ::None::
       preg_match($pattern, $data, $results);
       echo $results[1];


       $data2 = "::2015-06-02 10:13:54::None::";

       $pattern2 = "/::(.*?)::None::/"; // removed \d to remove digit restriction and added ? to capture anything present between :: and  ::None::
       preg_match($pattern2, $data2, $results);
       echo $results[1];
    ?>

输出: - https://eval.in/388254

答案 1 :(得分:1)

尝试这些模式。

$pattern = "/None::(.*?)::None::/";


$pattern2 = "/::(.*?)::None::/";