php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #75069 preg_match function wrong result
Submitted: 2017-08-13 13:07 UTC Modified: 2017-08-13 13:44 UTC
From: bublik dot drdrdr at gmail dot com Assigned:
Status: Not a bug Package: *Regular Expressions
PHP Version: 7.1.8 OS: Windows
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: bublik dot drdrdr at gmail dot com
New email:
PHP Version: OS:

 

 [2017-08-13 13:07 UTC] bublik dot drdrdr at gmail dot com
Description:
------------
preg_match finds a wrong match with these params:
pattern "(?=.*?[A-Za-z]).{8,}",
subject "вasdaв". Last contains 6 chars (english and russian), but regex pattern must accept only 8+ chars.

Test script:
---------------
$out = array();
preg_match("/(?=.*?[A-Za-z]).{8,}/", "вasdaв", $out);
var_dump($out);

Expected result:
----------------
array(0) { }

Actual result:
--------------
array(1) { [0]=> string(8) "вasdaв" }

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2017-08-13 13:14 UTC] spam2 at rhsoft dot net
can you tell why you are using preg_match() when you pretty clear deal with multibyte data to start with? the 2 multibyte chars are 2 bytes and 6+2 = 8

http://php.net/manual/en/function.mb-ereg-match.php
 [2017-08-13 13:44 UTC] nikic@php.net
-Status: Open +Status: Not a bug
 [2017-08-13 13:44 UTC] nikic@php.net
preg_match() is the correct function to use, you just need to specify the /u modifier if you're working with UTF-8. Please do not recommend the use of mb_ereg_match() unless it is strictly necessary.

Using "/(?=.*?[A-Za-z]).{8,}/u" will produce the desired result.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Mon Dec 30 14:01:28 2024 UTC