Home > Backend Development > PHP Tutorial > Regular expression functions compatible with Perl (2)

Regular expression functions compatible with Perl (2)

巴扎黑
Release: 2023-03-07 16:30:02
Original
1669 people have browsed it

Function preg_grep()

This function is different from the preg_match() function and preg_match_all() in that it matches the elements in the array and returns the array unit that matches the regular expression, The syntax format of this function is as follows:

array preg_grep(string pattren,array input)
Copy after login

This function returns an array, which includes the cells in the second parameter input array that match the given first parameter pattern pattern. Only one match is made for each element in the input array input. The code example for using this function is as follows:

<?php
$preg = &#39;^d{3,4}-?\d{7,8}&#39;;
$arr = array(&#39;043212345678&#39;,&#39;0431-7654321&#39;,&#39;12345678&#39;);
$preg_arr = preg_grep($preg,$arr);
var_dump($preg_arr);
?>
Copy after login

Match the phone number with the correct format (010-1234****, etc.) in the array $arr and save it to another array.

String processing functions strstr(), strpos(), strrpos(), substr()

If you just find whether a string contains a certain substring , it is recommended to use the strstr() or strpos() function. If you simply remove a substring from a string, it is recommended to use the substr() function. Although the string processing functions provided by PHP cannot complete complex string matching, the execution efficiency of processing some simple string matching is slightly higher than using regular expressions.

The function strstr() searches for the first occurrence of a string in another string, and the function returns the rest of the string (from the matching point). Returns FALSE if the searched string is not found. This function is case-sensitive. If you need to perform a case-insensitive search, you can use the stristr() function. This function has two parameters, the first parameter provides the string to be searched, and the second parameter is the string to be searched. If the parameter is a number, it searches for characters matching the numeric ASCII value. The usage code of this function is as follows:

<?php
echo strstr("this is a test!","test");
echo strstr("this is a test!",115);
?>
Copy after login

The function strpos() returns the position of the first occurrence of a string in another string. If the string is not found, it returns false. The function strrpos() is similar to the function strpos() and is used to find the last occurrence of a string in another string. These two functions are case-sensitive. If you need to perform a case-insensitive search, you can use the stripos() and strripos() functions. The function substr() can return a part of the string.

The above is the detailed content of Regular expression functions compatible with Perl (2). For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template