2017-05-19 71 views
-1

对不起,不能把标题正确地把我的问题,我找不到一个方法来问它。如何替换所有匹配的正则表达式的PHP

所以我试着去替换里面的所有文字和'在PHP更换为'

我有一个变量$desc = "he's such a good person and he'll be very successfull";

,我尝试做以下

$desc = str_replace("'","'",$desc); 

但是在str_replace中没有办法使用正则表达式?

是的,现在看起来很好...出于某种原因。

有没有办法使用正则表达式呢?

例如从文本中删除html标签?

$desc = "<strong> he&#39;s such a good person </strong> <br /> he&#39;ll be very successfull";

$desc = str_replace("<*>"," ",$desc);

+2

它工作正常。试试这个https://eval.in/800942 –

+0

它工作正常...? – samiles

+1

您必须在当前行之前的某处使用'htmlentities'。使用'html_entity_decode'将实体解码为它们的文字表示。 –

回答

0

是否必须是一个正则表达式,因为这是一个简单得多的方式来做到这一点

$desc = "he&#39;s such a good person and he&#39;ll be very successfull"; 

$new = str_replace('&#39;', "'", $desc); 
echo $new; 

结果:

he's such a good person and he'll be very successfull 
2

你可以试试要使用正确的PHP函数来完成这项工作,请使用al ook:preg_replace doc。

在你的情况,你想用这样的:

preg_replace('/&#39;/', "'", $desc); 

看看这个执行: https://eval.in/800948

+0

这可以用于'preg_replace('<*>',“”,$ desc);'? –

+0

当然@JuanfriLira你可以这样使用它:'preg_replace('/ \ <.*\> /','“,$ desc);'替换”<" and ">“之间的文字。 – Sense

0

你并不需要使用正则表达式,因为它会更复杂 使用

$desc = "he&#39;s such a good person and he&#39;ll be very  successfull"; 
$desc = str_replace("&#39;","'",$desc); 
echo "after replace :"." ".$desc;  

更简单:)

相关问题