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?
confusion about regular expressions in php
Moderator: General Moderators
-
- Forum Newbie
- Posts: 1
- Joined: Sun Jan 24, 2016 3:40 am
Re: confusion about regular expressions in php
$ only matches the end of a line if you pass the /m flag.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 ) )
Code: Select all
/l...$/m
It isn't doing that... You sure you got that right?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 ( ) )
Re: confusion about regular expressions in php
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]
/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]