Home > Backend Development > PHP Tutorial > php string replacement method

php string replacement method

高洛峰
Release: 2023-03-02 15:14:01
Original
1594 people have browsed it

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

Related labels:
php
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