简体   繁体   English

在mongodb中搜索并将字符串替换为特殊字符

[英]search and replace string with special characters in mongodb

I am using mongodb 2.4 with cakephp and as there is no direct way to search and replace string in array in mongo. 我正在将mongodb 2.4与cakephp一起使用,因为没有直接方法可以在mongo中搜索和替换数组中的字符串。 I am looping through all records and using str_replace to replace string with special charcaters('/','+','-') etc. But it is not working if more than one special charcter appears in a string. 我遍历所有记录,并使用str_replace用特殊字符('/','+','-')等替换字符串。但是,如果一个字符串中出现多个特殊字符,则无法正常工作。 Kindly help me out in using str_replace or another way to find and replace in mongo 请帮助我使用str_replace或另一种在mongo中查找和替换的方法

One way could be using regex, 一种方法是使用正则表达式,

<?php
    echo preg_replace('/(\/|\+|-)+/i', ' ', $string);
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM