Home > Article > Backend Development > php string replacement method
There is a content type field that stores language. Because this data is imported from Excel, so for the sake of simplicity, many languages are separated by commas, and all languages are made into a string, so that it is simple to save. Much. However, due to the problem of data quality at the beginning, some "Chinese" was "Mandarin Chinese". Now it is necessary to change all "Mandarin Chinese" 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);
}
}
?>