• 5
name

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 191

Backtrace:

File: /home/prodcxja/public_html/questions/application/views/question.php
Line: 191
Function: _error_handler

File: /home/prodcxja/public_html/questions/application/controllers/Questions.php
Line: 433
Function: view

File: /home/prodcxja/public_html/questions/index.php
Line: 315
Function: require_once

name Punditsdkoslkdosdkoskdo

What does [\S\s]* mean in regex in PHP?

What is meant by [\s\S]* in regex in PHP? Does [\s\S]* actually match every string the same as .*?

By default . doesn't match new lines - [\s\S] is a hack around that problem.
This is common in JavaScript, but in PHP you can use the /s flag to to make the dot match all characters.

  • 52
Reply Report

The . meta character matches any character except a newline. So the pattern .* which is used to match anything will not work if you have to match newlines as-well.

preg_match('/^.*$/',"hello\nworld"); // returns 0

[\s\S] which is a character class of white-space characters and non-whitespace characters matches any character including a newline so do [\d\D], [\w\W]. So your pattern [\s\S]* now matches anything.

preg_match('/^[\s\S]$/s',"hello\nworld"); // returns 1

An alternative to make . match anything (including a newline) is to use a s modifier.

preg_match('/^.*$/s',"hello\nworld"); // returns 1 

Alternative way of using the s modifier is in-lining it as:

preg_match('/^(?s).*(?-s)$/',"hello\nworld"); // returns 1

(?s) turns on the s mode and (?-s) turns if off. Once turned off any following . will not match a newline.

  • 21
Reply Report

Warm tip !!!

This article is reproduced from Stack Exchange / Stack Overflow, please click

Trending Tags

Related Questions