这里是PHP正则表达式示例的列表。也许这可以帮助某人,因为admin /或其他用户无法明确说明我正在尝试共享我的方法。
[preg_match执行搜索(preg_replace是替换程序。]]
preg_match具有三个参数-preg_match(FindWhat,FindWhere,GivingOutput);
示例1)
:<?php // Everything expect letters and numbers $text = 'abc345fg@h'; $newfilename = preg_match('/[^a-zA-Z0-9.]/', $text, $out); echo $out[0]; ?> Output will be: @
preg_match仅找到一个结果(最先发现的结果),具有两个选项:[0]或1。
示例2)
:查找我们搜索条件内的所有内容(任何字符,单词..):<?php $text = 'abcdefghijklmnopqrst'; $newfilename = preg_match('/ij(.*?)mn/', $text, $out); echo $out[0]; echo $out[1]; ?> [1] -gives only the inner search result (what we had in the brackets, between "ij" and "mn"): kl [0] -gives the whole search result: ijklmn
((请注意,如果您没有在搜索条件中使用方括号(如上面的示例1所示,则选项1不可用]
示例3)
:如果您的目标文本多次出现,例如:$ text ='你好用户Jimmy Jones
,是我。您好用户Mery Pawders,仍然是我。';现在,这是两个不同的匹配项,因此,我们需要使用preg_match_all
:在许多可能性中进行搜索:<?php $text = 'Hello user Jimmy Jones, it\'s me. Hello user Mery Pawders, it\'s me.'; $newfilename = preg_match_all('/hello user (.*?) it\'s/', $text, $out); foreach ($out[1] as $found_one) { echo $found_one; } // Or use $out[0] for full search match ?> Output will be: Jimmy Jones, Mery Pawders,
示例4)
:要查找字符串,而输入文本包含换行符,则必须在末尾使用s;<?php $text = 'member ACCOUNT7'; preg_match("/ACCOUNT[123456789]/", $text, $out); echo $out[1]; ?> Output will be: ACCOUNT7
示例5)
:搜索始终区分大小写。要进行大小写不敏感搜索,请在末尾使用i(如果需要,不使用s);<?php $text = 'one two three'; preg_match("/one(.*?)three/s", $text, $out); echo $out[1]; ?> Output will be: two
示例6)
:要在preg_match中搜索特殊字符(例如/".<?php $text = 'ONE TWO TREE'; preg_match("/one(.*?)three/si", $text, $out); echo $out[1]; ?>
示例7)
<?php $text = 'hello Jimmy/Kroger '; preg_match("/Jimmy\/Kroger/", $text, $out); echo $out[0]; ?>
现在,我们可以使用^运算符,它反过来搜索结果。
示例8)
:查找所有内容,而不是字母和数字:和replace,我们有一些不同的结构,因为我们需要使用新变量。<?php $text = 'abc@*&^)($%'; preg_match_all('/[^a-zA-Z0-9.]/', $text, $out); foreach ($out[0] as $varr) { echo $varr; } ?> Output will be: @*&^)($%
对于search
示例9)
:使用以下运算符查找并替换所有而不是字母和数字为其他字符:^:搜索并在找到的结果中添加一些内容:<?php $text = 'ab2sq)(&*(%$%^$@%n23f9'; $variable = preg_replace('/[^a-zA-Z0-9.]/', 'a', $text); echo $variable; ?> Output will be: ab2sqn23f9
示例10)
:查找文本内的所有链接:<?php $text = 'Hi, it\'s me, Niko from Austria'; $variable = preg_replace('/(Niko.*?) from/', '$1 Gomez', $text); echo $variable; ?> Output will be: it's me, Niko Gomez Austria
示例11)
:类似于示例11(但带有替换)-在文本中查找链接并将其放在锚定标签中:<?php $text = 'hi, my site is http://example.com, and on my page, at http://example.com/page37/blabla.html I wrote something..'; preg_match_all("/[[:alpha:]]+:\/\/[^<>[:space:]]+[[:alnum:]\/]/",$text, $out); foreach($out[0] as $varr){ echo $varr; } ?> Output will be: http://example.com http://example.com/page37/blabla.html
示例12)
<?php $text = 'Hi, my site is http://example.com, and on my page, at http://example.com/page37/trid.html I wrote something..'; $variable = preg_replace("/[[:alpha:]]+:\/\/[^<>[:space:]]+[[:alnum:]\/]/",'<a href="\\0">\\0</a>', $text); echo $variable; ?>
输出将是相同的句子,但是链接将被锚定。
1] 提示:如果只想检查一个字符串是否包含在另一个字符串中,请不要使用preg_match()。请改用stristr()或strpos(),因为它们会更快。
2)**有关PHP正则表达式的更高级的特定示例,请使用Google或参阅**[全部选项和手册
--http://www.php.net/manual/en/reference.pcre.pattern.syntax.php((您可以在此处快速查看所有运营商列表-http://www.catswhocode.com/blog/15-php-regular-expressions-for-web-developershttp://www.noupe.com/php/php-regular-expressions.html)
3)对于HTML代码,存在一种特殊的轻量级PHP软件,称为DOM Parser。但是有时候,如果您非常了解PHP正则表达式,则可能不需要DOM解析器。
这里是PHP正则表达式示例的列表。也许这可以帮助某人,因为admin /或其他用户无法明确说明我正在尝试共享我的方法。 preg_match执行...
尝试使用此正则表达式:
我假设,当您说“ $ email”时,您的意思是@
符号之前的内容。在这种情况下,您可以使用此正则表达式: