PHP preg_match_all is a search-based function in the PHP programming language. It returns the total number of matches found in a string. It also uses the regular expression pattern as a parameter to complete the search process. This function is very while we must extract the phone number from a given string, including some textual information. We can perform various other matching using this built-in PHP function. It provides all matching as an array output as preg_match_all includes all in its name (suffix). preg_match() is the singular version of this function that gives only a single finding as an output.
ADVERTISEMENT Popular Course in this category PHP DEVELOPER - Specialization | 8 Course Series | 3 Mock TestsStart Your Free Software Development Course
Web development, programming languages, Software testing & others
The syntax of PHP preg_match_all is given below:
preg_match_all(regexPattern, inputString, matches, flags, offset)
Parameter:
The user of preg_match_all()
$string = "WELCOME TO INDIA"; // string $patternRex = "/E/i"; // regular expression or the pattern if(preg_match_all($patternRex, $string, $matches)) { echo "<pre class="brush:php;toolbar:false">"; print_r($matches); }
In the above line of the code, we are searching for ‘E’ character. ‘i’ is being used in the regular expression for the not case sensitive. We will explore and see various other examples in the example section of this article.
Following are examples of the php preg_match_all are given below:
Searching ‘E’ character in the given string irrespective of the case of that string.
Code:
<?php $string = "WELCOME TO INDIA."; // string $patternRex = "/E/i"; // regular expression or the pattern $matchFound = preg_match_all($patternRex, $string, $matches); if($matchFound){ echo "<pre class="brush:php;toolbar:false">"; print_r($matches); } ?>
Output:
Checked whether ‘E’ character (capital letter of E) exists in the given string or not.
Code:
<?php $string = "Lorem Ipsum is simply dummy text of the printing and typesetting industry."; // string $patternRex = "/E/"; // regular expression or the pattern $matchFound = preg_match_all($patternRex, $string, $matches); if($matchFound){ echo "<pre class="brush:php;toolbar:false">"; echo "Match Found."; print_r($matches); }else{ echo "No match found."; } ?>
Output:
The ‘e’ character exists, as we can see in the string of the above example code, but the case is not matching as per the written in the pattern. No match found will be output, but if we change how to search for in-case sensitive, it will give some output.
Extracting all phone numbers from a given string.
Code:
<?php $string = "Hi, please call us at 800-222-1212 or 800-001-1212 or 700-555-1212 or 800-555-1312"; // string $patternRex = "/\b\d{3}\s*-\s*\d{3}\s*-\s*\d{4}\b/"; // regular expression or the pattern $matchFound = preg_match_all($patternRex, $string, $matches); if($matchFound){ echo "<pre class="brush:php;toolbar:false">"; echo "Match Found.<br>"; print_r($matches); }else{ echo "No match found."; } ?>
Output:
List the HTML tag element of a given string of various tags.
Code:
<?php $userinfo = "Name: <b>John Raw</b> Title: <b>PHP Lover</b> Name: <b>Shree Sham</b> Title: <b>JAVA Lover</b> Name: <b>Jonathan</b> Title: <b>Node Lover</b> Name: <b>Amla Palekar </b> Title: <b>Comic Reader</b>"; preg_match_all ("/<b>(.*)<\/b>/U", $userinfo, $matches); for ($i=0; $i< count($matches[0]); $i++) { echo $matches[0][$i]."<br>"; } ?>
Output:
As we can see in the above example code, the code extracts all the tag with the value of the tag. is usually used for making the string bold in the HTML. In the same way, we can try other elements of the combination of factors to get the desired result per our business requirements.
A program to count the number of spaces of a given string using the preg_match_all() function.
Code:
<?php $userinfo = "The code below count the number of spaces of the given string."; $search = preg_match_all ("/\\s/", $userinfo, $matches); if($search){ echo "Number of spaces: ".count($matches[0]); } ?>
The above line of code shows that we have a string containing spaces. We have a regular expression to match the pattern of the area. We can see all the rooms inside the $matches array if the games are found. Then we use the count() function to get the count of that $matches array. We can use this PHP pattern-based PHP search function in various other ways.
Output:
내장 함수 preg_match_all()은 정규 표현식을 사용하여 문자열에 있는 모든 항목을 검색할 수 있습니다. 이 기능은 단순히 검색 결과를 제공하는 것이 아니라 검색 결과를 배열로 제공합니다. 개발자나 코더는 이 정규식 기능을 사용하여 비즈니스 요구 사항에 따라 다양한 요구 사항을 충족할 수 있습니다. 때로는 일치하는 배열의 출력으로 다차원 배열을 제공하므로 개발자는 이 기능을 사용할 때 주의해야 합니다.
위 내용은 PHP preg_match_all의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!