dongqiang8474 2016-10-17 02:53
浏览 76
已采纳

PHP 5.5 preg_replace使用返回null的模式数组

I've looked at other questions on this similar topic, and tried those suggestions but they don't seem to work.

This is my code:

<?php

$badChars = array('/</', '/>/', '/$/', '/\\/', '/=/', '/@/', '/\//');

$cleanData = "Text -with /stuff I don@'t want";

echo $cleanData . "
";

$cleanData = preg_replace($badChars, '', $cleanData);

echo $cleanData . "
";

?>

Note that the array of patterns will vary based on the scenario. This is for a data cleansing exercise. e.g.: if processing an email field, we'd temporarily drop the @ pattern.

And this is the output:

Text -with /stuff I don@'t want

PHP Warning:  preg_replace(): No ending delimiter '/' found in /home/tim/xm_code/symphony/code/components/controllers/test.php on line 9

Process finished with exit code 0

I can't find anything to help me resolve this. Any ideas?

  • 写回答

1条回答 默认 最新

  • dtf24224 2016-10-17 03:07
    关注

    Your double backslash is escaping your closing delimiter. So

    '/\\/'
    

    needs to be

    '/\\\/'
    

    Alternatively use a character class and you don't need the array. Second alternative, use str_replace since the characters are all static.

    $badChars = array('<', '>', '$', '\\', '=', '@', '/');
    $cleanData = "Text -with /stuff I don@'t want";
    echo $cleanData . "
    ";
    $cleanData = str_replace($badChars, '', $cleanData);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办