Php正则表达式不替换文本

时间:2018-01-02 18:54:14

标签: php

正则表达式不会替换内容。

$post_content = '<p>M-Kavach is a mobile device security solution which focuses on various threats related to android mobile device. It is an initiative by indian government . As you all know the Modi government took the decision to demonetize Rs 500 and Rs 1000 notes. And a digital india campaign has been started on a very vast scale. So india is going digital. There are also threats to it . More hackers and cyber criminals would evolve. So to save Indian government has taken an initiative Cyber Swachhta Kendra (botnet cleaning and malware analysis centre). Indian government has spent Rs 90 crores on Cyber Swachhta Kendra taking care of digital india. Currently we have so called antivirus software only for android device but keep visiting this site because soon we are going to post PC antivirus software by indian government.&lt;br&gt;&lt;br&gt;</p>
<p><img src="source/pankaj260/codexworld.png?1514909769678" alt="codexworld" width="502" height="477" /></p>
<p>Features of Mkavach are :&lt;br&gt;&lt;br&gt;<br />Restricted access to critical applications&lt;br&gt;<br />Hardware resource control in terms of access to WiFi, Bluetooth, Camera and MobileData&lt;br&gt;<br />Intimates unauthorized SIM changes to trusted mobile number through SMS&lt;br&gt;<br />Remote wipe of Contacts &amp; Call-Logs using SMS&lt;br&gt;<br />Option to Factory Reset the device remotely using SMS&lt;br&gt;<br />Blocks unwanted Calls &amp; SMS&lt;br&gt;<br />Easy backup &amp; restore&lt;br&gt;<br />Protects against JavaScript Malware&lt;br&gt;&lt;br&gt;<br />&lt;a 
<img src="source/pankaj260/codexworld.png?1514909769678" alt="codexworld" width="502" height="477" /></p>
href="https://play.google.com/store/apps/details?id=org.cdac.mkavach" target="_blank"&gt;Download Mkavach&lt;/a&gt;&lt;br&gt;<br />&lt;b&gt; Do visit for pc anti virus by indian government&lt;b&gt;</p>';

preg_replace('(src="source\/)','(src="admin\/source\/)',$post_content);

3 个答案:

答案 0 :(得分:3)

由于您不需要使用正则表达式来完成替换,因此请考虑使用str_ireplace()。这是指向manual的链接。

您的代码将类似:

str_ireplace('src="source/','src="admin/source/)',$post_content);

答案 1 :(得分:0)

新字符串由preg_replace()返回,因此您需要将其分配给变量:

$post_content = preg_replace('(src="source\/)','(src="admin\/source\/)',$post_content);

答案 2 :(得分:0)

你不必在第二个参数上逃避/,否则你会得到这个结果:

source/ ===替换为===&gt; admin\/source\/

最后,您必须将preg_replace的结果分配给变量(或将其传递给某个函数...)

$post_content = preg_replace('(src="source\/)','(src="admin/source/)',$post_content);

请注意,要对字符串执行简单的查找/替换,您只需使用 str_replace

$post_content = str_replace( 'src="source/', 'src="admin/source/)', $post_content );