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.

http://php.net/manual/en/function.preg-match-all.php

Answer from romaninsh on Stack Overflow
🌐
PHP
php.net › manual › en › function.preg-match.php
PHP: preg_match - Manual
Any single character \s Any whitespace character \S Any non-whitespace character \d Any digit \D Any non-digit \w Any word character (letter, number, underscore) \W Any non-word character \b Any word boundary character (...) Capture everything enclosed (a|b) a or b a? Zero or one of a a* Zero or more of a a+ One or more of a a{3} Exactly 3 of a a{3,} 3 or more of a a{3,6} Between 3 and 6 of a options: i case insensitive m make dot match newlines x ignore whitespace in regex o perform #{...} substitutions only once ... Combining flags PREG_OFFSET_CAPTURE | PREG_UNMATCHED_AS_NULL will NOT result in a value of NULL for any submatched subpattern.
🌐
W3Schools
w3schools.com › php › func_regex_preg_match.asp
PHP preg_match() Function
The preg_match() function returns whether a match was found in a string.
Discussions

Should preg_match be checked if it's true or false?
This depends on context. preg_match / preg_match_all can return false or an integer , so technically you should check strictly for false or zero (depending on what you want to check) - see the red box on the manual page "Return Values" section. However, if the subject is known to be a string, and the regular expression pattern is not user supplied (and has been tested), I don't believe there's any case where it could return a failure (or at least the chances of / possible reasons for a failure are negligible). If the regular expression pattern contains untrusted input (is built from user input or other sources that could provide problematic values), then you should strictly check the return value. More on reddit.com
🌐 r/PHPhelp
4
1
July 11, 2023
preg match - PHP's preg_match() and preg_match_all() functions - Stack Overflow
What do the preg_match() and preg_match_all() functions do and how can I use them? More on stackoverflow.com
🌐 stackoverflow.com
[SOLVED] preg_match, preg_match_all and preg_grep
Hi folks, Sorry if I am too noob. I am trying to understand how all these functions work, and examples in php.net are not enough for me. What are the MAIN difference between these functions? For example, with $html below, what function I should use to find ALL html tags (, , &l... More on forums.phpfreaks.com
🌐 forums.phpfreaks.com
10
July 11, 2008
regex - How do I match this pattern using preg_match in PHP? - Stack Overflow
I'm writing a simple quiz engine in PHP and supply the question text in this format question|correct/feedback|wrong/feedback|wrong/feedback There can be as many wrong/feedback options as necessary... More on stackoverflow.com
🌐 stackoverflow.com
🌐
Phpliveregex
phpliveregex.com
PHP Live Regex
Test PHP regular expressions live in your browser and generate sample code for preg_match, preg_match_all, preg_replace, preg_grep, and preg_split!
🌐
Rexegg
rexegg.com › regex-php.php
PHP Regex Tutorial
If you include a variable's name as a third parameter, such as $match in the example below, when there is a match, the variable will be filled with an array: element 0 for the entire match, element 1 for Group 1, element 2 for Group 2, and so on. But a code box is worth a thousand words, so consider the following example. $subject='Give me 10 eggs'; $pattern='~\b(\d+)\s*(\w+)$~'; $success = preg_match($pattern, $subject, $match); if ($success) { echo "Match: ".$match[0]."<br />"; echo "Group 1: ".$match[1]."<br />"; echo "Group 2: ".$match[2]."<br />"; } Output: Match: 10 eggs Group 1: 10 Group 2: eggs Notice how $match[0] contains the overall match?
🌐
Regex101
regex101.com › r › 5VyM27 › 1
regex101: PHP preg_match
Detailed match information will be displayed here automatically.
Find elsewhere
Top answer
1 of 3
151

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.

http://php.net/manual/en/function.preg-match-all.php

2 of 3
20

Both preg_match and preg_match_all functions in PHP use Perl compatible regular expressions.

You can watch this series to fully understand Perl compatible regular expressions: https://www.youtube.com/watch?v=GVZOJ1rEnUg&list=PLfdtiltiRHWGRPyPMGuLPWuiWgEI9Kp1w

preg_match($pattern, $subject, &$matches, $flags, $offset)

The preg_match function is used to search for a particular $pattern in a $subject string and when the pattern is found the first time, it stops searching for it. It outputs matches in the $matches, where $matches[0] will contain the text that matched the full pattern, $matches[1] will have the text that matched the first captured parenthesized sub-pattern, and so on.

Example of preg_match()

<?php
preg_match(
    "|<[^>]+>(.*)</[^>]+>|U",
    "<b>example: </b><div align=left>this is a test</div>",
    $matches
);

var_dump($matches);

Output:

array(2) {
  [0]=>
  string(16) "<b>example: </b>"
  [1]=>
  string(9) "example: "
}

preg_match_all($pattern, $subject, &$matches, $flags)

The preg_match_all function searches for all the matches in a string and outputs them in a multi-dimensional array ($matches) ordered according to $flags. When no $flags value is passed, it orders results so that $matches[0] is an array of full pattern matches, $matches[1] is an array of strings matched by the first parenthesized sub-pattern, and so on.

Example of preg_match_all()

<?php
preg_match_all(
    "|<[^>]+>(.*)</[^>]+>|U",
    "<b>example: </b><div align=left>this is a test</div>",
    $matches
);

var_dump($matches);

Output:

array(2) {
  [0]=>
  array(2) {
    [0]=>
    string(16) "<b>example: </b>"
    [1]=>
    string(36) "<div align=left>this is a test</div>"
  }
  [1]=>
  array(2) {
    [0]=>
    string(9) "example: "
    [1]=>
    string(14) "this is a test"
  }
}
🌐
Reintech
reintech.io › blog › exploring-php-preg-match-function-for-pattern-matching
Exploring the Power of PHP's `preg_match()` Function for Pattern Matching
April 14, 2023 - Dive into PHP's preg_match() function, a powerful tool for pattern matching using regular expressions. Learn its syntax, usage, and examples to harness its full potential.
🌐
GeeksforGeeks
geeksforgeeks.org › php › php-preg_match-function
PHP | preg_match() Function - GeeksforGeeks
July 11, 2025 - <?php // Declare a variable and initialize it $gfg = "GFG is the best Platform."; // case-Insensitive search for the word "GFG" if (preg_match("/\bGFG\b/i", $gfg, $match)) echo "Matched!"; else echo "not matched"; ?>
🌐
PHP Tutorial
phptutorial.net › home › php tutorial › php preg_match
PHP preg_match
September 18, 2021 - The preg_match() finds the string for a match to a regular expression.
🌐
PHP Freaks
forums.phpfreaks.com › php coding › regex help
[SOLVED] preg_match, preg_match_all and preg_grep - Regex Help - PHP Freaks
July 11, 2008 - Hi folks, Sorry if I am too noob. I am trying to understand how all these functions work, and examples in php.net are not enough for me. What are the MAIN difference between these functions? For example, with $html below, what function I should use to find ALL html tags (, , &l...
🌐
Scaler
scaler.com › home › topics › php preg_match() function
PHP preg_match() function - Scaler Topics
June 14, 2023 - The preg_match function in PHP returns an integer value, which represents the number of matches found between the regular expression pattern and the string being searched.
🌐
SitePoint
sitepoint.com › php
How to format php preg_match() regular expression pattern - PHP - SitePoint Forums | Web Development & Design Community
November 15, 2020 - Hi I am trying to create a PHP preg_match() to validate a string to the following restrictions:- Must contain 1 lower, 1 upper and 1 number Must contain alphanumeric ONLY Must NOT contain spaces or special characters Must be 8 - 15 characters in length I have managed to accomplish most of the ...
🌐
OnlinePHP
onlinephp.io › preg-match
preg_match - Online Tool
PHP Functions · Regular Expressions · preg_match · Execute preg_match with this online tool preg_match() - Perform a regular expression match · Preg Match Online Tool · Manual · Code Examples · Preg Match Online Tool · Manual · Code Examples · preg_filter ·
Top answer
1 of 4
1

That is a regular expression.

The '^' matches the beginning of a string.

The '\D' matches any character that is not a digit.

The '\d' matches any digit.

The '\s' matches any whitespace.

The plus sign means that the previous character can occur multiple times.

So basically it would match all those lines in your file, except that last comma.

Blue = 1 = No = 20

That line would match the regex.

About your last question to allow numbers too, use this:

/^(.+)\s=\s(\d+)\s=\s(\D+)\s=\s(\d+)/
2 of 4
1

the code is a regular expression:

/^(\D+)\s=\s(\d+)\s=\s(\D+)\s=\s(\d+)/

the code will use the regular expression to cut the string um pieces and put in an array ($matches)

preg_match('/^(\D+)\s=\s(\d+)\s=\s(\D+)\s=\s(\d+)/', trim($row), $matches);

You shall use the code to see better

print_r($matches)

To find by name or by item number change the code

if(strstr($matches[1], $query))

to

if(isset($matches[1]) && (strstr($matches[1], $query) || $matches[2] == $query) )

Your code shall look like this...

if (isset($_GET['id'])) {
$itemid = $_GET['id'];
$search = "$itemid";
$query = ucwords($search);
$string = file_get_contents('http://clubpenguincheatsnow.com/tools/newitemdatabase/items.php');
if($itemid=="")
{
echo "Please fill out the form.";
}
else
{
$string = explode('<br>',$string);
foreach($string as $row)
{
preg_match('/^(\D+)\s=\s(\d+)\s=\s(\D+)\s=\s(\d+)/', trim($row), $matches);
if(isset($matches[1]) && (strstr($matches[1], $query) || $matches[2] == $query) )

{
echo "<a href='http://clubpenguincheatsnow.com/tools/newitemdatabase/info.php?id=$matches[2]'>";
echo $matches[1];
echo "</a><br>";
}
}
}
}
else {
echo "Item does not exist!";
}
🌐
Functions-Online
functions-online.com › preg_match.html
test preg_match online - regular expression PHP functions - functions-online
Searches $subject for all matches to the regular expression given in $pattern and puts them in $matches in the order specified by $flags. int preg_match ( string $pattern , string $subject , array &$matches [, int $flags ] [, int $offset ] )