PHP之正则表达式函数-PHP问题

资源魔 59 0


后面的话

  正则表白式不克不及自力应用,它只是一种用来界说字符串的规定模式,必需正在相应的正则表白式函数中使用,能力完成对字符串的婚配、查找、交换及宰割等操作。后面引见了正则表白式的根底语法,本文将具体引见正则表白式函数

婚配与查找

【preg_match()】

  preg_match()函数用来执行一个正则表白式婚配,搜寻subject与pattern给定的正则表白式的一个婚配。前往pattern的婚配次数。它的值将是0次(没有婚配)或1次,由于preg_match()正在第一次婚配后将会中止搜寻。preg_match_all()没有同于此,它会不断搜寻subject直到抵达末端。假如发作谬误preg_match()前往FALSE

int preg_match ( string $pattern , string $subject [, array &$matches [, int $flags = 0 [, int $offset = 0 ]]] )

  pattern示意要搜寻的模式,字符串类型

  subject示意输出字符串

  假如提供了参数matches,它将被填充为搜寻后果。$matches[0]将蕴含完好模式婚配到的文本, $matches[1] 将蕴含第一个捕捉子组婚配到的文本,以此类推

  flags能够被设置为如下标志:一、PREG_OFFSET_CAPTURE。假如通报了这个标志,关于每个呈现的婚配前往时会附加字符串偏偏移量(绝对于指标字符串的)。留意:这会扭转填充到matches参数的数组,使其每一个元素成为一个由第0个元素是婚配到的字符串,第1个元素是该婚配字符串正在指标字符串subject中的偏偏移量;二、offset。通常,搜寻从指标字符串的开端地位开端。可选参数offset用于指定从指标字符串的某个未知开端搜寻(单元是字节)

<?php
//从URL中猎取主机称号
preg_match('@^(?:http://)?([^/]+)@i',    "http://www.php.net/index.html", $matches);
$host = $matches[1];//猎取主机称号的前面两局部
preg_match('/[^.]+.[^.]+$/', $host, $matches);//domain name is: php.netecho "domain name is: {$matches[0]}";
?>
<?php
 $pattern = '/www.[^./]+.com/i';
$subject = 'www.百度.com,www.qq.com,www.cnblogs.com';preg_match($pattern,$subject,$matches);/*array (size=1)  0 => string 'www.百度.com' (length=13) */var_dump($matches);
?>

【preg_match_all()】

  preg_match_all()与preg_match()相似,没有同的是preg_match()正在第一次婚配之后就会中止搜寻,而函数preg_match_all()则会不断搜寻到指定字符串的末端,能够猎取到一切婚配到的后果

int preg_match_all ( string $pattern , string $subject [, array &$matches [, int $flags = PREG_PATTERN_ORDER [, int $offset = 0 ]]] )
<?php 
$pattern = '/www.[^./]+.com/i';$subject = 'www.百度.com,www.qq.com,www.cnblogs.com';
preg_match_all($pattern,$subject,$matches);
/*
array (size=1)  0 => 
    array (size=3)      0 => string 'www.百度.com' (length=13)      1 => string 'www.qq.com' (length=10)      2 => string 'www.cnblogs.com' (length=15)
 */
var_dump($matches);
?>
【preg_grep()】

  preg_grep()前往给定命组input中与模式pattern 婚配的元素组成的数组

array preg_grep ( string $pattern , array $input [, int $flags = 0 ] )

  假如flags设置为PREG_GREP_INVERT,这个函数前往输出数组中与 给定模式pattern没有婚配的元素组成的数组

<?php 
$pattern = '/www.[^./]+.com/i';
$subject = ['百度.com','www.qq.com','www.cnblogs.com'];var_dump (preg_grep($pattern,$subject));
?>

交换

【preg_replace()】

 preg_replace()执行一个正则表白式的搜寻交换,搜寻subject婚配pattern的局部,以replacement进行交换
mixed preg_replace ( mixed $pattern , mixed $replacement , mixed $subject [, int $limit = -1 [, int &$count ]] )

  replacement示意用于交换的字符串或字符串数组。假如这个参数是一个字符串,而且pattern是一个数组,那末一切的模式都应用这个字符串进行交换。假如pattern以及replacement都是数组,每一个pattern应用replacement中对应的元素进行交换。假如replacement中的元素比pattern中的少,多进去的pattern应用空字符串进行交换

<?php
$string = 'April 15, 2016';$pattern = '/(w+) (d+), (d+)/i';
$replacement = '${1}1,$3';//April1,2016echo preg_replace($pattern, $replacement, $string);
?><?php
$string = 'The quick brown fox jumped over the lazy dog.';
$patterns = array();
$patterns[0] = '/quick/';
$patterns[1] = '/brown/';
$patterns[2] = '/fox/';$replacements = array();
$replacements[2] = 'bear';$replacements[1] = 'black';
$replacements[0] = 'slow';//The bear black slow jumped over the lazy dog.echo preg_replace($patterns, $replacements, $string);
?>
【preg_replace_callback()】

  preg_replace_callback()执行一个正则表白式搜寻而且应用一个回调进行交换

mixed preg_replace_callback ( mixed $pattern , callable $callback , mixed $subject [, int $limit = -1 [, int &$count ]] )
<?php
// 将文本中的年份添加一年.
$text = "April fools day is 04/01/2002";
$text.= "Last christmas was 12/24/2001";
// 回调函数
function next_year($matches){  // 通常: $matches[0]是实现的婚配  // $matches[1]是第一个捕捉子组的婚配  // 以此类推  return $matches[1].($matches[2]+1);}>

【preg_filter()】

  preg_filter() 执行一个正则表白式搜寻以及交换,等价于preg_replace()除了了它仅仅前往(可能通过转化)与指标婚配的后果

mixed preg_filter ( mixed $pattern , mixed $replacement , mixed $subject [, int $limit = -1 [, int &$count ]] )
<?php
$subject = array('1', 'a', '2', 'b', '3', 'A', 'B', '4'); $pattern = array('/d/', '/[a-z]/', '/[1a]/'); $replace = array('A:$0', 'B:$0', 'C:$0'); ?>

宰割

【preg_split()】
  preg_split()经过一个正则表白式分隔字符串
array preg_split ( string $pattern , string $subject [, int $limit = -1 [, int $flags = 0 ]] )

  假如指定limit,将限度分隔失去的子串最多只有limit个,前往的最初一个子串将蕴含一切残余局部。limit值为-1,0或null时都代表"没有限度";能够应用null跳过对flags的设置

  flags能够是任何上面标志的组合(以位或运算 | 组合):PREG_SPLIT_NO_EMPTY——假如这个标志被设置,preg_split()将进前往分隔后的非空局部;PREG_SPLIT_DELIM_CAPTURE——假如这个标志设置了,用于分隔的模式中的括号表白式将被捕捉并前往;PREG_SPLIT_OFFSET_CAPTURE——假如这个标志被设置,关于每个呈现的婚配前往时将会附加字符串偏偏移量。留意:这将会扭转前往数组中的每个元素,使其每一个元素成为一个由第0个元素为分隔后的子串,第1个元素为该子串正在subject中的偏偏移量组成的数组

<?php//应用逗号或空格(蕴含" ", , , 
, f)分隔短语$keywords = preg_split("/[s,]+/", "hypertext language, progra妹妹ing");/*Array
(
    [0] => hypertext
    [1] => language
    [2] => progra妹妹ing
) */print_r($keywords);?>

本义

【preg_quote()】

  preg_quote()本义正则表白式字符

string preg_quote ( string $str [, string $delimiter = NULL ] )

  正则表白式非凡字符有: . + * ? [ ^ ] $ ( ) { } = ! < > | : -

<?php$keywords = '$40 for a g3/400';$keywords = preg_quote($keywords, '/');echo $keywords; // 前往 $40 for a g3/400?>

以上就是前端学PHP之正则表白式函数的全副内容。

相干保举:资源魔

以上就是PHP之正则表白式函数的具体内容,更多请存眷资源魔其它相干文章!

标签: php php教程 php故障解决 php使用问题

抱歉,评论功能暂时关闭!