开发者

php, how to search and replace in mysql database?

i am trying to search my database for the string <strong&g开发者_如何学编程t;some text here</strong> and replace it with s<strong>ome text here</strong>

basically find the first <strong> then find the first character and place it in front of the <strong>

maybe use something like here

is this complex enough? :)

thanks


There is no regexp replace support in MySQL so you'll have to do the replacing in PHP and then do the update (or implement some sort of UDF for MySQL, see How to do a regular expression replace in MySQL?).

In PHP you'll use something like (after fetching all rows into a $rowset):

foreach($rowset as $row) {
  $replacement=preg_replace('/<strong>(.)/','\1<strong>',$row->fieldname,1);
  myDBCall('update mytable set myfield="'.$replacement.'" where someid='.$row->someid);
}


You would use something like:

update `table` set `fieldname` = replace(`fieldname`,'string_to_find','string_to_replace_with')
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜