Home > Backend Development > PHP Tutorial > PHP string replacement method_PHP tutorial

PHP string replacement method_PHP tutorial

WBOY
Release: 2016-07-21 15:22:14
Original
833 people have browsed it

In the past few days, I encountered a small problem at work. There is a content type field that stores language. Because the data is imported from Excel, so in order to simplify the processing, many languages ​​​​are separated by commas, and all languages ​​​​are A string, which makes saving it much simpler. However, due to the problem of data quality at the beginning, some "Chinese" were "Mandarin Chinese", and now all "Mandarin Chinese" need to be changed to "Chinese".
This requires replacing part of a string. For such a problem, we usually use regular expressions to do the replacement, or use some replacement methods in PHP. It feels very troublesome to do this, and time is tight, so I thought of a lazy way, using the explode function. , use the string to be replaced as the delimiter, then connect the string to be replaced between the two array elements, and then UPDATE.

Copy code The code is as follows:

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);
}
}
?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/324767.htmlTechArticleIn the past few days, I encountered a small problem at work. There is a content type field that stores language, because this Writing data is done using Excel import, so in order to simplify the processing, many languages ​​​​use...
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 Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template