Home > php教程 > php手册 > body text

php 字符串替换的方法

WBOY
Release: 2016-06-06 20:39:40
Original
928 people have browsed it

使用explode函数,把要替换的字符串做为分割符号,然 后把两个数组元素中间接上要换成的字符串,然后UPDATE一下就行了

这几天,工作中遇到一个小问题,有一内容类型的字段存储的是语言,因为这写数据是用Excel导入做的,所以为了处理简单,很多的语言就用逗号分隔,把所有语言做一个字符串,这样存入的时候就简单的多。但是由于当初数据质量的问题,有一部分“Chinese”是 “Mandarin Chinese”,现在需要把所有的“Mandarin Chinese”改为“Chinese”。
这就需要把一个字符串中的部分字符串替换掉。对于这样的问题,一般就是用正则表达式来做替换,或者使用PHP的一些替换的方法,这样做的话感觉很比较麻烦,时间又比较紧,所以就想了这样一个懒人办法,使用explode函数,把要替换的字符串做为分割符号,然后把两个数组元素中间接上要换成的字符串,然后UPDATE一下就行了。
代码如下:
function replace(){
$sql = db_query("SELECT field_languages_value,nid FROM {content_type_company_profile} WHERE

field_languages_value like '%Mandarin Chinese%'");
while($result = db_fetch_object($sql)){
$a = explode("Mandarin Chinese",$result->field_languages_used_value);
$b = $a[].'Chinese'.$a[1];
db_query("UPDATE content_type_company_profile SET field_languages_used_value = '%s' WHERE nid = %

d",$b,$result->nid);
}
}
?>
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!