What does [Ss]* mean in regex in PHP?

The regular expression [Ss]* in PHP matches any character (including whitespace and newlines) zero or more times. The character class [Ss] combines \S (non-whitespace characters) and \s (whitespace characters), effectively matching any character in the string.

Basic Usage

Here's how [Ss]* works in practice ?

<?php
$pattern = '/[Ss]*/';
$text = "Hello\nWorld with spaces";

if (preg_match($pattern, $text, $matches)) {
    echo "Match found: " . $matches[0];
} else {
    echo "No match found";
}
?>
Match found: Hello
World with spaces

Comparison with Dot Modifier

The [Ss]* pattern serves as an alternative to using the dot with the s flag ?

<?php
$text = "Line 1\nLine 2\nLine 3";

// Using [Ss]* - matches everything including newlines
preg_match('/[Ss]*/', $text, $matches1);
echo "Using [Ss]*: " . strlen($matches1[0]) . " characters<br>";

// Using .* without s flag - stops at newline
preg_match('/.*/', $text, $matches2);
echo "Using .*: " . strlen($matches2[0]) . " characters<br>";

// Using .* with s flag - matches everything like [Ss]*
preg_match('/.*/s', $text, $matches3);
echo "Using .* with s flag: " . strlen($matches3[0]) . " characters<br>";
?>
Using [Ss]*: 21 characters
Using .*: 6 characters
Using .* with s flag: 21 characters

Practical Example

Extracting content between HTML tags that spans multiple lines ?

<?php
$html = "<div>
    <p>First paragraph</p>
    <p>Second paragraph</p>
</div>";

// Extract content between div tags
if (preg_match('/<div>([Ss]*)<\/div>/', $html, $matches)) {
    echo "Content found:<br>" . trim($matches[1]);
}
?>
Content found:
<p>First paragraph</p>
    <p>Second paragraph</p>

Conclusion

The [Ss]* pattern is useful when you need to match any character including newlines without using the s modifier. It's particularly handy for parsing multi-line content or when working with text that contains line breaks.

Updated on: 2026-03-15T08:50:33+05:30

487 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements