replace ereg_replace with preg_replace [duplicate]

…衆ロ難τιáo~ 提交于 2020-01-09 04:56:46

问题


Hi need to change the function ereg_replace("[\]", "", $theData) to preg_replace


回答1:


To port ereg_replace to preg_replace you need to put the regex between a pair of delimiter

Also your regx is [\] is invalid to be used for preg_replace as the \ is escaping the closing char class ]

The correct port is

preg_replace('/[\\\]/','',$theData) 

Also since the char class has just one char there is no real need of char class you can just say:

preg_replace('/\\\/','',$theData) 

Since you are replace just a single char, using regex for this is not recommended. You should be using a simple text replacement using str_replace as:

str_replace('\\','',$data);



回答2:


str_replace("\\","",$theData);

But I seriously doubt you need that replace at all. most likely you need some other operation.
What is this replace for?




回答3:


preg_replace("/\\\/", "", $theData);



回答4:


I used this sed to automatically replace ereg_replace by preg_replace and put the required slashes in. Assumes no \" in the first regex

 sed -i 's#ereg_replace("\([^"]*\)"#preg_replace("/\1/"#g' *.php


来源:https://stackoverflow.com/questions/3649574/replace-ereg-replace-with-preg-replace

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!