Page 1 of 1

confusion about regular expressions in php

Posted: Sun Jan 24, 2016 4:14 am
by Muhammad Zeeshan
Can anybody explain me about the following code.
The following code
<?php
$a = 'first line
second line
';
$b = '/l...$/';
$match = preg_match_all($b,$a,$matches);

print_r($matches);

?>
is showing the output

Array ( [0] => Array ( ) )

instead of
Array ( [0] => Array ( [0] => line ) )

But the following code

<?php
$a = 'first line
second line
';
$b = '/l....$/';
$match = preg_match_all($b,$a,$matches);

print_r($matches);

?>
is showing
Array ( [0] => Array ( [0] => line ) )
instead of

Array ( [0] => Array ( ) )
Can anyone explain me why it is working in reverse order in my computer?

Re: confusion about regular expressions in php

Posted: Sun Jan 24, 2016 4:36 am
by requinix
Muhammad Zeeshan wrote:The following code
<?php
$a = 'first line
second line
';
$b = '/l...$/';
$match = preg_match_all($b,$a,$matches);

print_r($matches);

?>
is showing the output

Array ( [0] => Array ( ) )

instead of
Array ( [0] => Array ( [0] => line ) )
$ only matches the end of a line if you pass the /m flag.

Code: Select all

/l...$/m
Muhammad Zeeshan wrote:But the following code

<?php
$a = 'first line
second line
';
$b = '/l....$/';
$match = preg_match_all($b,$a,$matches);

print_r($matches);

?>
is showing
Array ( [0] => Array ( [0] => line ) )
instead of

Array ( [0] => Array ( ) )
It isn't doing that... You sure you got that right?

Re: confusion about regular expressions in php

Posted: Wed May 11, 2016 3:31 pm
by b03tz
The one with 3 dots is matching line, the with 4 isn't. At least not on regex101.com

/l...$/m matches "line" (on the first line)
/l...$/ matches "line" (on the second line)

And:
/l....$/m and /l....$/ matches nothing, so indeed, are you sure you got that right?

[edit] wow old thread nvm [/edit]