简体   繁体   English

简单的preg_replace

[英]Simple preg_replace

I cant figure out preg_replace at all, it just looks chinese to me, anyway I just need to remove " &page-X " from a string if its there. 我根本找不到preg_replace,它看起来只是中文给我,无论如何我只需要从字符串中删除“ &page-X ”。

X being a number of course, if anyone has a link to a useful preg_replace tutorial for beginners that would also be handy! X是一个课程,如果有人有一个有用的preg_replace教程的链接,初学者也会很方便!

Actually the basic syntax for regular expressions, as supported by preg_replace and friends, is pretty easy to learn. 实际上, preg_replace和朋友支持的正则表达式的基本语法非常容易学习。 Think of it as a string describing a pattern with certain characters having special meaning. 可以将其视为描述具有特殊含义的某些字符的模式的字符串。

In your very simple case, a possible pattern is: 在您非常简单的情况下,可能的模式是:

&page-\d+

With \\d meaning a digit (numeric characters 0-9) and + meaning: Repeat the expression right before + (here: \\d ) one or more times. \\d表示数字(数字字符0-9)和+含义:在+ (此处: \\d )之前重复表达式一次或多次。 All other characters just represent themselves. 所有其他角色只代表自己。

Therefore, the pattern above matches any of the following strings: 因此,上面的模式匹配以下任何字符串:

&page-0
&page-665
&page-1234567890

Since the preg functions use a Perl-compatible syntax and regular expressions are denoted between slashes ( / ) in Perl, you have to surround the pattern in slashes: 由于preg函数使用Perl兼容语法,并且在Perl中的斜杠( / )之间表示正则表达式,因此必须用斜杠包围模式:

$after = preg_replace('/&page-\d+/', '', $before);

Actually, you can use other characters as well: 实际上,您也可以使用其他字符:

$after = preg_replace('#&page-\d+#', '', $before);

For a full reference of supported syntax, see the PHP manual . 有关支持的语法的完整参考,请参阅PHP手册

preg_replace uses Perl-Compatible Regular Expression for the search pattern. preg_replace使用Perl兼容的正则表达式作为搜索模式。 Try this pattern: 试试这种模式:

preg_replace('/&page-\d+/', '', $str)

See the pattern syntax for more information. 有关更多信息,请参阅模式语法

$outputstring = preg_replace('/&page-\d+/', "", $inputstring);

的preg_replace()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM