2013-10-18 48 views
0

我想查看“公司结算”这个短语,并且它对整个短语不区分大小写(例如,应该选择“公司结算”,“公司结算“,”CoMpAny Billing“等)。想要匹配不区分大小写的preg_replace的短语

我在这里的代码似乎不想捡起来。我敢肯定,我在那里有一些不正确的正则表达式:

if (preg_match("/company billing\b/i", $post['message']) !== false) { 
    $post['message'] = preg_replace('/billing\b/i', 'Company Billing', $post['message']); 
} 
+0

什么是您的输入字符串?你有什么结果? – Toto

+1

你为什么要对'$ string'进行匹配,但是替换为'$ post ['messsage']'?不应该是'$ _POST'而不是'$ post'? – Barmar

+0

我忘了把它放在$ post ['message']部分。 – user1449369

回答

0

这应该是足够好:

if (preg_match("/\bcompany billing\b/i", $string)) { 
    // handle match success case 
} 
else { 
    // handle match failure case 
} 

preg_match返回0,如果匹配失败

时它才会返回FALSE发生错误。

+0

你不需要'!'。 – Barmar

+0

@Barmar:如果块是针对OP代码中的失败案例的,则 – anubhava

+0

OP的代码处理成功案例。 – Barmar

1

除非您的RegEx需要比此后变得更加复杂,str_ireplace()在这里看起来更好。因为如果在字符串中找不到任何形式的“公司结算”,将不会做出任何更改:

$post['message'] = str_ireplace("company billing", "Company Billing", $post['message']); 
+0

是的,这是正确的。我的错。 – user1449369

+0

这就是我现在拥有的。然而,它似乎忽略了短语中的“计费”部分,而不是寻找“公司帐单”这个WHOLE短语 – user1449369

+0

它可能只是另一个小的错字,但这是:$ post ['message'] = preg_replace('/ billing \ b/i','公司结算',$ post ['message']);可能需要更改为:$ post ['message'] = preg_replace('/ company billing \ b/i','Company Billing',$ post ['message']); ...前者将用“公司公司结算”替换“公司结算”。后者将取代它只是“公司账单” – Will

相关问题