Friday, October 2, 2020

PHP preg_match_all() Function

​​


Hello dear readers! welcome back to another section of my tutorial on PHP. In this tutorial guide, we will be discussing about the PHP preg_match_all() Function. 

PHP preg_match_all() function matches all the occurrences of pattern in a string.

It will place all these matches in the array pattern_array in the order you specify using the optional input parameter order. There are two possible types of order, they are -

  • PREG_PATTERN_ORDER - It is the default if the optional order parameter isn't added. The PREG_PATTERN_ORDER specifies the order in a way that you might think most logical; $pattern_array[0] is an array of all the complete pattern matches in the code, $pattern_array[1] is an array of all strings matching the first parenthesized regexp.
  • PREG_SET_ORDER - This is going to order the array a bit differently than the default setting. $pattern_array[0] is going to contain elements that are matched by the first parathesized regexp in your code, the $pattern_array[1] is going to contain elements that are matched by the next parenthesized regexp, etc.

RECOMMENDED POST: PHP eregi_replace() Function

Syntax

The following is the syntax for the preg_match_all() function -

string preg_match_all (string pattern,  string string,  array pattern_array [,  int order] );

Return Value

  • This returns the numbers of matchings.

Example

Following is a simple example -

<?php
   $userinfo = "Name: <b>Bethel Prince</b> <br> Title: <b>PHP Guru</b>";
   preg_match_all ("/<b>(.*)<\/b>/U", $userinfo, $pat_array);
   
   print $pat_array[0][0]." <br> ".$pat_array[0][1]."\n";
?>

Output

When the above code is executed, it will produce the following result -

Bethel Prince 
PHP Guru

RECOMMENDED POST: PHP preg_match() Function

Alright guys! This is where we are rounding up for this tutorial post. In my next tutorial guide, we are going to be discussing about the PHP preg_replace() Function.

Feel free to ask your questions where necessary and i will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.

Follow us on our various social media platforms to stay updated with our latest tutorials. You can also subscribe to our newsletter in order to get our tutorials delivered directly to your emails.

Thanks for reading and bye for now.
Share:

0 comments:

Post a Comment

Hello dear readers! Please kindly try your best to make sure your comments comply with our comment policy guidelines. You can visit our comment policy page to view these guidelines which are clearly stated. Thank you.