PHP preg_replace函数
我想用preg_replace对字符串进行一些更改。 我正在使用正则表达式,但我的代码不起作用 例如:PHP preg_replace函数,php,regex,Php,Regex,我想用preg_replace对字符串进行一些更改。 我正在使用正则表达式,但我的代码不起作用 例如: The quick brown fox jumps over the lazy dog 我想更改字符串中包含以下内容的所有部分: 'The quick brown (REGEX - ALL CHARACTERS: fox, dog, cow) jumps over the lazy dog' 到 如何删除这些字符?您可以使用正向的前向和后向断言删除介于棕色和跳跃之间的任何字符 $strin
The quick brown fox jumps over the lazy dog
我想更改字符串中包含以下内容的所有部分:
'The quick brown (REGEX - ALL CHARACTERS: fox, dog, cow) jumps over the lazy dog'
到
如何删除这些字符?您可以使用正向的前向和后向断言删除介于棕色和跳跃之间的任何字符
$string = "The quick brown fox jumps over the lazy dog";
echo preg_replace("/(?<=brown).*(?=jumps)/", "", $string);
这将捕获brown并跳入$1和$2组,然后在替换字符串中使用它们,而不使用中间部分
使用任一方法的输出:
The quick brownjumps over the lazy dog
您可以使用以下内容:
$str = "The quick brown (REGEX - ALL CAHRACTE: fox, dog, cow) jumps over the lazy dog";
preg_replace('/(The quick brown )(.*)( jumps over the lazy dog)/', '$1$3', $str);
检查正则表达式的一个很好的地方是:您想用这样的空值替换fox吗
The quick brown jumps over the lazy dog
然后简单地这样做:
<?php
$string = 'The quick brown fox jumps over the lazy dog';
$pattern = '/\bfox\b/i';
$new_string = preg_replace($pattern, null, $string);
echo $new_string;
?>
请详细说明正则表达式。。。部分它到底包含什么?有三个字母吗?没有,我知道开始是快速棕色,结束是跳过懒狗,但这两部分之间可能就是一切。我想从字符串中删除此文本。快速棕色+?+跳过懒惰的人dog@GiorgiTabatadze:除换行符外的所有内容都是+?您可以编辑您的问题以显示所需的输出吗?
The quick brown jumps over the lazy dog
<?php
$string = 'The quick brown fox jumps over the lazy dog';
$pattern = '/\bfox\b/i';
$new_string = preg_replace($pattern, null, $string);
echo $new_string;
?>