Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

preg_match :print: class matches tab character

for($i = 0; $i < 255; $i++)
    if (preg_match('@[[:print:]]@', chr($i))) echo "chr($i) matches :print:<br>"; else echo "chr($i) doesnt match :print:<br>";

On my Windows system, the output for chr(9) is:

chr(9) matches :print:

With the same code, on a Linux system, the output is:

chr(9) doesnt match :print:

Why does the :print: class match \t on Windows only?

  • PHP 5.5.12
like image 708
mynetx Avatar asked May 19 '14 19:05

mynetx


People also ask

How do you match a tab in regex?

Use \t to match a tab character (ASCII 0x09), \r for carriage return (0x0D) and \n for line feed (0x0A).

What is the difference between Preg_match and Preg_match_all?

preg_match stops looking after the first match. preg_match_all , on the other hand, continues to look until it finishes processing the entire string. Once match is found, it uses the remainder of the string to try and apply another match.

What does Preg_match mean?

Definition and Usage The preg_match() function returns whether a match was found in a string.

What is the purpose of Preg_match () regular expression in PHP?

The preg_match() function will tell you whether a string contains matches of a pattern.


1 Answers

It could be a locale issue, but then you must have a very funky locale on your hands. POSIX says that no cntrl can be print, and no space or blank can be print unless it's the space character itself. In the "C" locale, tab is all of cntrl, space, and blank; it seems like a very strange locale that would consider it none of those.

like image 159
hobbs Avatar answered Oct 20 '22 04:10

hobbs