php preg_PHP Preg简介

php preg

Preg_Grep PHP函数 ( Preg_Grep PHP Function )

The PHP function, preg_grep, is used to search an array for specific patterns and then return a new array based on that filtering. There are two ways to return the results. You can return them as is, or you can invert them (instead of only returning what matches, it would only return what does not match). It is phrased as: preg_grep ( search_pattern, $your_array, optional_inverse ).The search_pattern needs to be a regular expression. If you are unfamiliar with them this article gives you an overview of the syntax.

PHP函数preg_grep用于在数组中搜索特定模式,然后基于该过滤返回一个新数组。 有两种返回结果的方法。 您可以按原样返回它们,也可以将它们取反(而不是只返回匹配的内容,它只会返回不匹配的内容)。 它的措辞为: preg_grep(search_pattern,$ your_array,optional_inverse)。search_pattern必须是一个正则表达式。 如果您不熟悉它们,本文将为您提供语法概述。

This code would result in the following data:Array ( [4] => 4 [5] => 5 )Array ( [3] => three [6] => six [9] => nine )

该代码将导致以下数据:Array([4] => 4 [5] => 5)Array([3] =>三个[6] =>六个[9] =>九)

First, we assign our $data variable. This is a list of numbers, some in alpha form, others in numeric. The first thing we run is called $mod1. Here we are searching for anything that contains 4, 5, or 6. When our result is printed below we only get 4 and 5, because 6 was written as 'six' so it did not match our search.

首先,我们分配$ data变量。 这是数字列表,一些以字母形式,另一些以数字形式。 我们运行的第一件事称为$ mod1。 在这里,我们正在搜索包含4、5或6的任何内容。当结果打印在下面时,我们仅得到4和5,因为6被写为“ 6”,因此它与我们的搜索不匹配。

Next, we run $mod2, which is searching for anything that contains a numeric character. But this time we include PREG_GREP_INVERT. This will invert our data, so instead of outputting numbers, it outputs all of our entries that were not numeric (three, six and nine).

接下来,我们运行$ mod2,它会搜索包含数字字符的任何内容。 但是这次我们包含了PREG_GREP_INVERT 。 这将反转我们的数据,因此,它不输出数字,而是输出我们所有非数字的条目(三个,六个和九个)。

Preg_Match PHP函数 ( Preg_Match PHP Function )

The Preg_MatchPHP function is used to search a string and return a 1 or 0. If the search was successful a 1 will be returned, and if it was not found a 0 will be returned. Although other variables can be added, it is most simply phrased as: preg_match(search_pattern, your_string). The search_pattern needs to be a regular expression.

的preg_match PHP函数用来搜索字符串,并返回一个1或0。如果成功了1将返回的搜索,如果未找到则返回0。 尽管可以添加其他变量,但其最简单的表述为: preg_match(search_pattern,your_string) 。 search_pattern必须是一个正则表达式。

The code above uses preg_match to check for a key word (first juice then egg) and replies based on whether it is true (1) or false (0). Because it returns these two values, it is most often used in a conditional statement.​

上面的代码使用preg_match来检查一个关键字(先榨汁然后是鸡蛋),然后根据其是对(1)还是对(0)进行答复。 由于它返回这两个值,因此最常用于条件语句中

Preg_Match_All PHP函数 ( Preg_Match_All PHP Function )

Preg_Match_All is used to search a string for specific patterns and stores the results in an array. Unlike preg_match which stops searching after it finds a match, preg_match_all searches the entire string and records all matches. It is phrased as: preg_match_all (pattern, string, $array, optional_ordering, optional_offset).

Preg_Match_All用于在字符串中搜索特定的模式并将结果存储在数组中。 与preg_match会在找到匹配项后停止搜索不同, preg_match_all搜索整个字符串并记录所有匹配项。 它的措辞为: preg_match_all(模式,字符串,$ array,optional_ordering,optional_offset)

In our first example, we use PREG_PATTERN_ORDER. We are searching for 2 things; one is the time, the other is it's am/pm tag. Our results are outputted to $match, as an array where $match[0] contains all matches, $match[1] contains all data matching our first sub-search (the time) and $match[2] contains all data matching our second sub-search (am/pm).

在第一个示例中,我们使用PREG_PATTERN_ORDER。 我们正在寻找2件东西; 一个是时间,另一个是时间标签。 我们的结果输出到$ match,作为一个数组,其中$ match [0]包含所有匹配项,$ match [1]包含与第一个子搜索(时间)匹配的所有数据,而$ match [2]包含与我们的第一个子搜索匹配的所有数据第二个子搜索(上午/下午)。

In our second example we use PREG_SET_ORDER. This puts each full result into an array. The first result is $match[0], with $match[0][0] being the full match, $match[0][1] being the first sub-match and $match[0][2] being the second sub-match.

在第二个示例中,我们使用PREG_SET_ORDER。 这会将每个完整结果放入数组中。 第一个结果是$ match [0],其中$ match [0] [0]是完全匹配,$ match [0] [1]是第一个子匹配,$ match [0] [2]是第二个子匹配子比赛。

Preg_Replace PHP函数 ( Preg_Replace PHP Function )

The preg_replace function is used to do a find-and-replace on a string or an array. We can give it one thing to find and replace (for example it seeks out the word 'him' and changes it to 'her'), or we can give it a full list of things (an array) to search for, each with a corresponding replacement. It is phrased as preg_replace ( search_for, replace_with, your_data , optional_limit, optional_count ) The limit will default to -1, which is no limit. Remember your_data can be a string or an array.

preg_replace函数用于在字符串或数组上进行查找和替换。 我们可以给它一件事来查找和替换(例如,它找到单词“ him”并将其更改为“ her”),或者我们可以给它提供要搜索的事物的完整列表(一个数组),每个事物都有相应的替换。 它的短语preg_replace(search_for,replace_with,your_data,optional_limit,optional_count),该限制默认为-1,没有限制。 请记住,your_data可以是字符串或数组。

In our first example, we simply replace 'the' with 'a.' As you can see these are cAse seNsiTIvE. Then we set up an array, so in our second example, we are replacing both the words 'the' and 'cat.' In our third example, we set the limit to 1, so each word is only replaced one time. Finally, ​in our 4th example, we keep count of how many replacements we have made.

在我们的第一个示例中,我们仅将“ the”替换为“ a”。 如您所见,这是案例的敏感之处。 然后,我们建立了一个数组,因此在第二个示例中,我们同时替换了单词“ the”和“ cat”。 在第三个示例中,我们将限制设置为1,因此每个单词仅被替换一次。 最后,在我们的第四个示例中,我们记录了已经进行了多少次替换。

Preg_Split PHP函数 ( Preg_Split PHP Function )

The function Preg_Spilit is used to take a string and put it into an array. The string is broken up into different values in the array based upon your input. It is phrased as preg_split ( split_pattern, your_data, optional_limit, optional_flags )

函数Preg_Spilit用于获取字符串并将其放入数组中。 根据您的输入,字符串在数组中分为不同的值。 它被表述为preg_split(split_pattern,your_data,optional_limit,optional_flags)

In the code above we perform three splits. In our first, we split the data by each character. In the second, we split it with a blank space, thus giving each word (and not each letter) an array entry. And in our third example, we use a '.' period to split the data, therefore giving each sentence its own array entry.

在上面的代码中,我们执行三个拆分。 在我们的第一篇文章中,我们按每个字符拆分数据。 在第二个中,我们用空格将其分割,从而为每个单词(而不是每个字母)提供一个数组条目。 在第三个示例中,我们使用“。” 时间段分割数据,因此为每个句子提供自己的数组条目。

Because in our last example we use a '.' period to split, a new entry is started after our final period, so we add the flag PREG_SPLIT_NO_EMPTY so that no empty results are returned. Other available flags are PREG_SPLIT_DELIM_CAPTURE, which also captures the character you are splitting by (our "." for example) and PREG_SPLIT_OFFSET_CAPTURE, which captures the offset in characters where the split has occurred.

因为在最后一个示例中,我们使用了“。” 要分割的时间段,在最后一个时间段之后将开始一个新的条目,因此我们添加了标志PREG_SPLIT_NO_EMPTY,以便不返回任何空结果。 其他可用标志是PREG_SPLIT_DELIM_CAPTUREPREG_SPLIT_OFFSET_CAPTURE,它捕获发生分割的字符的偏移量(例如,我们的“。”)。

Remember that the split_pattern needs to be a regular expression and that a limit of -1 (or no limit) is the default if none is specified.

请记住,split_pattern必须是一个正则表达式,如果未指定限制,则默认值为-1(或无限制)。

翻译自: https://www.thoughtco.com/introduction-to-preg-in-php-2693795

php preg

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值