Namita
Namita

Reputation: 75

simple preg_match help needed

I am very new to php and need someone help. From the code below, I want the line "I am a good boy" but it prints "I am a good boy::None::She is a good girl"

$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];

and in another one, I want "2015-06-02 10:13:54" but I get "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];

Upvotes: 0

Views: 36

Answers (2)

Death-is-the-real-truth
Death-is-the-real-truth

Reputation: 72289

A bit change in 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];
    ?>

Output:- https://eval.in/388254

Upvotes: 2

PovertyBob
PovertyBob

Reputation: 31

Try these patterns.

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


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

Upvotes: 1

Related Questions