欢迎来到重庆建网站公司-满荣网络,开始您的互联网之旅!
641408888
189-0831-3333
当前位置:重庆建网站 > 新闻资讯 > SEO优化方案 > 网站制作开始使用PHP正则表达式

网站制作开始使用PHP正则表达式

作者:重庆建网站   发布时间:2018-01-30 10:06:51    阅读次数:918   
  网站制作开始使用PHP正则表达式重庆网站建设

  1。正则表达式是什么?

  正则表达式的主要目的,也被称为正则表达式或regexp,是有效地搜索模式在一个给定的文本。这些搜索模式是使用正则表达式解析器理解的特殊格式编写的。

  正则表达式是从UNIX系统,其中设计了一个程序,调用grep,帮助用户处理字符串和文本操作。通过遵循几个基本规则,可以创建非常复杂的搜索模式。

  举个例子,假设你被赋予了检查电子邮件或电话号码是否正确的任务。使用一些简单的命令,由于正则表达式,这些问题可以很容易地得到解决。语法一开始并不总是那么简单,但是一旦你学会了它,你就会意识到你可以很容易地完成相当复杂的搜索,只需输入几个字符,你就可以从不同的角度处理问题。

  网站制作开始使用PHP正则表达式

  2。兼容正则表达式库

  PHP实现了相当多的正则表达式功能,使用不同的分析引擎。有两个主要的PHP解析器。一个叫POSIX和PCRE兼容正则表达式或其他。

  POSIX的PHP函数的前缀是ereg_。自发布的PHP 5.3这台发动机是过时的,但让我们更优更快PCRE引擎一看。

  在PHP PCRE函数一开始preg_如preg_match或preg_replace。您可以在PHP文档中读取完整的函数列表。

  3.基本语法

  要使用正则表达式首先需要学习语法。该语法由一系列字母、数字、点、连字符和特殊标志,我们可以一起使用不同的括号。

  在PHP中,每个正则表达式模式都被定义为使用Perl格式的字符串。在Perl,一个正则表达式模式是写在斜杠,如/你好/。在PHP中这将成为一个字符串,“你好”。

  Now,let’s have a look at some operators,the basic building blocks of regular expressions

  Operator Description

  ^The circumflex symbol marks the beginning of a pattern,although in some cases it can be omitted

  $Same as with the circumflex symbol,the dollar sign marks the end of a search pattern

  .The period matches any single character

  ?It will match the preceding pattern zero or one times

  +It will match the preceding pattern one or more times

  *It will match the preceding pattern zero or more times

  |Boolean OR

  –Matches a range of elements

  ()Groups a different pattern elements together

  []Matches any single character between the square brackets

  {min,max}It is used to match exact character counts

  d Matches any single digit

  D Matches any single non digit caharcter

  w Matches any alpha numeric character including underscore(_)

  W Matches any non alpha numeric character excluding the underscore character

  s Matches whitespace character

  As an addition in PHP the forward slash character is escaped using the simple slash.Example:‘/he/llo/’

  To have a brief understanding how these operators are used,let’s have a look at a few examples:

  Example Description

  ‘/hello/’It will match the word hello

  ‘/^hello/’It will match hello at the start of a string.Possible matches are hello or helloworld,but not worldhello

  ‘/hello$/’It will match hello at the end of a string.

  ‘/he.o/’It will match any character between he and o.Possible matches are helo or heyo,but not hello

  ‘/he?llo/’It will match either llo or hello

  ‘/hello+/’It will match hello on or more time.E.g.hello or hellohello

  ‘/he*llo/’Matches llo,hello or hehello,but not hellooo

  ‘/hello|world/’It will either match the word hello or world

  ‘/(A-Z)/’Using it with the hyphen character,this pattern will match every uppercase character from A to Z.E.g.A,B,C…

  ‘/[abc]/’It will match any single character a,b or c

  ‘/abc{1}/’Matches precisely one c character after the characters ab.E.g.matches abc,but not abcc

  ‘/abc{1,}/’Matches one or more c character after the characters ab.E.g.matches abc or abcc

  ‘/abc{2,4}/’Matches between two and four c character after the characters ab.E.g.matches abcc,abccc or abcccc,but not abc

  除了操作符之外,还有正则表达式修饰符,它可以全局改变搜索模式的行为。重庆网站建设

  正则表达式修饰符放在模式,这样/你好/我和他们由单字母如我这标志着一个模式不区分大小写或X忽略空白字符。要获得修饰符的完整列表,请访问PHP的在线文档。

  正则表达式的真正力量依赖于合并这些操作符和修饰符,因此创建相当复杂的搜索模式。

  网站制作开始使用PHP正则表达式

  4.Using Regex in PHP

  In PHP we have a total of nine PCRE functions which we can use.Here’s the list:

  preg_filter–performs a regular expression search and replace

  preg_grep–returns array entries that match a pattern

  preg_last_error–returns the error code of the last PCRE regex execution

  preg_match–perform a regular expression match

  preg_match_all–perform a global regular expression match

  preg_quote–quote regular expression characters

  preg_replace–perform a regular expression search and replace

  preg_replace_callback–perform a regular expression search and replace using a callback

  preg_split–split string by a regular expression

  The two most commonly used functions are preg_match and preg_replace.

  Let’s begin by creating a test string on which we will perform our regular expression searches.The classical hello world should do it.

  $test_string='hello world';

  If we simply want to search for the word hello or world then the search pattern would look something like this:

  preg_match('/hello/',$test_string);

  preg_match('/world/',$test_string);

  如果我们想看看字符串是否以hello开头,我们只需在搜索模式的开头放置这个字符:

  preg_match('/^hello/',$test_string);

  请注意,正则表达式是区分大小写的,上面的模式将不匹配hello这个单词。如果我们希望我们的模式不区分大小写,我们应该应用下面的修饰符:

  preg_match('/^hello/i',$test_string);

  请注意在斜杠后面的模式后面的字符i。

  现在让我们来研究一个更复杂的搜索模式。如果我们要检查字符串中的前五个字符是alpha数字字符怎么办?。

  preg_match('/^[A-Za-z0-9]{5}/',$test_string);

  让我们来剖析这个搜索模式。首先,采用插入字符(^)我们指定的字符串必须以一个字母数字字符。这是由[就]指定。

  从A到Z的字母A-Z,其次是相同的除了小写字符的所有字符,这是很重要的,因为正则表达式是大小写敏感。我想你会明白自己什么0-9的手段。

  {5}只是告诉正则表达式分析器的准确计数五字。如果我们将六替换为五,解析器将不匹配任何东西,因为在我们的测试字符串中,hello是五个字符长,后面是空格字符,在我们的例子中是不计数的。

  此外,这个正则表达式可以优化为以下形式:

  preg_match('/^w{5}/',$test_string);

  w specifies any alpha numeric characters plus the underscore character(_).

  6。有用的正则表达式函数

  下面是一些使用正则表达式的PHP函数,您可以在日常中使用它们。

  验证电子邮件。这个函数将验证给定的电子邮件地址字符串,以确定它是否有正确的格式。

  function validate_email($email_address)

  {

  if(!preg_match("/^([a-zA-Z0-9])+([a-zA-Z0-9._-])* ([a-zA-Z0-9_-])+

  ([a-zA-Z0-9._-]+)+$/",$email_address))

  {

  return false;

  }

  return true;

  }

  Validate a URL

  function validate_url($url)

  {

  return preg_match('|^http(s)?://[a-z0-9-]+(.[a-z0-9-]+)*(:[0-9]+)?

  (/.*)?$|i',$url);

  }

  Remove repeated words.I often found repeated words in a text,such as this this.This handy function will remove such duplicate words.

  function remove_duplicate_word($text)

  {

  return preg_replace("/s(w+s)1/i","$1",$text);

  }

  Validate alpha numeric,dashes,underscores and spaces

  function validate_alpha($text)

  {

  return preg_match("/^[A-Za-z0-9_-]+$/",$text);

  }

  Validate US ZIP codes

  function validate_zip($zip_code)

  {

  return preg_match("/^([0-9]{5})(-[0-9]{4})?$/i",$zip_code);

  }

  7。正则表达式的小抄

  因为小抄现在是凉的,下面你可以找到一个小抄,可以运行通过,很快你忘了什么东西。

  以上信息来源网络,本文关键词:重庆网站建设
你希望我们为您提供什么服务

qq code