本文介绍了PHP preg_replace用三种不同的模式进行了三遍?对还是错?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个简单的问题:这是最好的方法吗?

A simple question: Is this the best way to do it?

$pattern1 = "regexp1";
$pattern2 = "regexp2";
$pattern3 = "regexp3";

$content = preg_replace($pattern1, '', $content);
$content = preg_replace($pattern2, '', $content);
$content = preg_replace($pattern3, '', $content);

我有三个要过滤的搜索模式!我上面的代码合适还是有更好的方法?

I have three search-patterns I want to filter out! Is my code above appropriate or is there a better way?

推荐答案

在用相同的内容替换所有内容时,您可以传递一个数组

As you are replacing all with the same, you could do either pass an array

$content = preg_replace(array($pattern1,$pattern2, $pattern3), '', $content);

或创建一个表达式:

$content = preg_replace('/regexp1|regexp2|regexp3/', '', $content);


如果表达式"实际上是纯字符串,请使用 str_replace .


If the "expressions" are actually pure character strings, use str_replace instead.

这篇关于PHP preg_replace用三种不同的模式进行了三遍?对还是错?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 13:09
查看更多