preg_grep($pattern, $input[, $flag = 0]):返回匹配模式的数组参数: $pattern:正则表达式,要搜索的模式 $input:输入的数组 $flag:当设置为PREG_GREP_INVERT,则搜索与指定模式pattern不匹配的数组元素
<?php
$arr = [ 1 , 2 , 3.4 , 5 , 6 , 7.8 , 9 ] ;
$preg = preg_grep ( "/^(\d+)?\.\d+$/" , $arr ) ;
print_r ( $preg ) ;
preg_match($pattern, $subject[, &$matches[, $flags = 0[, $offset = 0]]]):搜索 subject 与 pattern 给定的正则表达式的一个匹配参数: $pattern:正则表达式,要搜索的模式 $subject:输入的字符串 $matches:当提供了改参数时,将会填充匹配结果,$matches[0]将包含完整模式匹配到的文本, $matches[1] 将包含第一个捕获子组匹配到的文本,以此类推 $flag:当设置为PREG_OFFSET_CAPTURE时,对于每一个出现的匹配返回时会附加字符串偏移量 $offset:从目标字符串的某个位置开始返回值: 返回pattern的匹配次数,0或1次,发生错误则返回false
if ( preg_match ( "/\bweb\b/i" , "PHP is the web scripting language of choice." ) ) {
echo "查找到匹配的字符串。\n" ;
} else {
echo "未发现匹配的字符串。\n" ;
}
preg_match ( "/^(?:http:\/\/)?([^\/]+)/i" , "http://www.yyfs.com/index.html" , $matches ) ;
var_dump ( $matches ) ;
preg_match ( "/^(?:http:\/\/)?([^\/]+)/i" , "http://www.yyfs.com/index.html" , $matches , PREG_OFFSET_CAPTURE ) ;
echo '<pre>' ;
print_r ( $matches ) ;
echo '</pre>' ;
preg_match_all($pattern, $subject[, &$matches[, $flags = 0[, $offset = 0]]]):搜索 subject 与 pattern 给定的正则表达式的一个匹配参数:与preg_match基本类似 $flag: PREG_PATTERN_ORDER:结果排序为$matches[0]保存完整模式的所有匹配, $matches[1] 保存第一个子组的所有匹配,以此类推 PREG_SET_ORDER:结果排序为$matches[0]包含第一次匹配得到的所有匹配(包含子组), $matches[1]是包含第二次匹配到的所有匹配(包含子组)的数组,以此类推 PREG_OFFSET_CAPTURE:对于每一个出现的匹配返回时会附加字符串偏移量返回值: 返回pattern的匹配次数,0或n次,发生错误则返回false
$userinfo = "Name: <b>PHP</b> <br> Title: <b>Programming Language</b>" ;
preg_match_all ( "/<b>(.*)<\/b>/U" , $userinfo , $pat_array ) ;
print_r ( $pat_array [ 0 ] ) ;
preg_match ( "/^(?:http:\/\/)?([^\/]+)/i" , "http://www.yyfs.com/index.html" , $matches ) ;
var_dump ( $matches ) ;
$html = "<b>bold text</b><a href=howdy.html>click me</a>" ;
preg_match_all ( "/(<([\w]+)[^>]*>)(.*?)(<\/\\2>)/" , $html , $matches , PREG_SET_ORDER ) ;
var_dump ( $matches ) ;
preg_split($pattern, $subject[, $limit = -1[, $flags = 0]]):搜索 subject 与 pattern 给定的正则表达式的一个匹配参数: $pattern:正则表达式,要搜索的模式 $subject:输入的字符串 $limit:如果指定将限制分隔得到的子串最多只有limit个,最后的子串将包含剩余的所有部分,limit=-1/0/null时都代表不限制 $flag:PREG_SPLIT_NO_EMPTY:将返回分隔后的非空部分 PREG_SPLIT_DELIM_CAPTURE:用于分隔的模式中的括号表达式将被捕获并返回 PREG_OFFSET_CAPTURE:对于每一个出现的匹配返回时会附加字符串偏移量返回值: 返回一个使用 pattern 边界分隔 subject 后得到的子串组成的数组
$keywords = preg_split ( "/([\s,]+)/" , "hypertext language, programming" , - 1 , PREG_SPLIT_DELIM_CAPTURE ) ;
print_r ( $keywords ) ;
preg_replace($pattern, $replacement, $subject[, $limit = -1[, &$count]]):搜索 subject 与 pattern 给定的正则表达式的部分,用replacement替换参数: $pattern:正则表达式,要搜索的模式 $replacement:替换的字符串 $subject:输入的字符串 $limit:最大可替换数,-1 代表不限制 $count:替换执行的次数返回值: 当subject是一个数组时,返回一个匹配的数组,其他情况返回字符串,匹配错误返回null
$str = 'yy f s ' ;
$str = preg_replace ( '/\s+/' , '' , $str ) ;
echo $str ;
preg_quote($str, $replacementt[, &$delimiter = NULL]):转义正则表达式字符参数: $str:输入的字符串 $delimiter:如果指定了可选参数 delimiter,它也会被转义返回值: 返回转义后的字符串
$textbody = "This book is *very* difficult to find." ;
$word = "*very*" ;
$textbody = preg_replace ( "/" . preg_quote ( $word ) . "/" , "<i>" . $word . "</i>" , $textbody ) ;
echo $textbody ;
preg_replace_callback($pattern, $callback, $subject[, $limit = -1[, &$count]]):执行一个正则表达式搜索并且使用一个回调进行替换参数: $pattern:正则表达式,要搜索的模式 $callback:在每次需要替换时调用回调函数 $subject:输入的字符串 $limit:最大可替换数,-1 代表不限制 $count:替换执行的次数返回值: 当subject是一个数组时,返回一个匹配的数组,其他情况返回字符串,匹配错误返回null
$text = "April fools day is 04/01/2002\n" ;
$text . = "Last christmas was 12/24/2001\n" ;
function next_year ( $matches )
{
return $matches [ 1 ] . ( $matches [ 2 ] + 1 ) ;
}
echo preg_replace_callback ( "|(\d{2}/\d{2}/)(\d{4})|" , "next_year" , $text ) ;