Home Backend Development PHP Tutorial php 字符串替换的方法_PHP

php 字符串替换的方法_PHP

Jun 01, 2016 pm 12:14 PM
String replacement

这几天,工作中遇到一个小问题,有一内容类型的字段存储的是语言,因为这写数据是用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);
}
}
?>

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to replace the enter key in php How to replace the enter key in php Mar 23, 2023 am 11:12 AM

In PHP program development, sometimes text content needs to be processed, including replacing the Enter key. Below we will briefly introduce how to replace the Enter key in PHP.

How to replace a string starting with something with php regular expression How to replace a string starting with something with php regular expression Mar 24, 2023 pm 02:57 PM

PHP regular expressions are a powerful tool for text processing and conversion. It can effectively manage text information by parsing text content and replacing or intercepting it according to specific patterns. Among them, a common application of regular expressions is to replace strings starting with specific characters. We will explain this as follows

How to replace special characters in string using regular expression in PHP How to replace special characters in string using regular expression in PHP Jun 24, 2023 am 10:46 AM

With the continuous development of the Internet, PHP's application scenarios are becoming more and more extensive. In PHP development, sometimes you need to replace special characters in a string. In this case, you can use regular expressions for replacement. This article will introduce how to use regular expressions to replace special characters in strings in PHP. First, learn the basics of regular expressions. Regular expressions are a language used to describe patterns in some strings. Regular expressions include some special characters, such as ., *, +, ?, etc. These special characters have special meanings. in PH

How to replace string from left in php How to replace string from left in php Mar 23, 2023 pm 04:53 PM

PHP is a widely used scripting language that can be used to develop various web applications and websites. In these applications, strings are an integral part. In many cases, we need to perform operations such as replacing, splitting or intercepting strings. This article will explain how to replace a string from the left in PHP.

preg_replace() function in PHP: How to replace string using regular expression preg_replace() function in PHP: How to replace string using regular expression Nov 03, 2023 am 09:09 AM

preg_replace() function in PHP: How to replace a string using regular expressions, specific code examples are needed In PHP, the preg_replace() function is a very powerful and flexible function that allows us to search and replace characters using regular expressions string. Whether it is to remove specific characters from a string or replace text in a specific format, the preg_replace() function can help us achieve it easily. First, let's take a look at preg_replace()

How to replace the 11 digits after the colon in php How to replace the 11 digits after the colon in php Mar 21, 2023 pm 04:32 PM

Replacing strings is one of the most basic operations in PHP. The replacement function is crucial in data processing and string operations, and can improve program execution efficiency and performance. There are many functions for replacing strings in PHP, such as: substr_replace, str_replace, preg_replace and so on. But for specific string replacements, special replacement rules may need to be used. In this article, we will learn how to replace the 11 digits after the colon.

Use the strings.Replace function to replace substrings in a string and set the number of replacements Use the strings.Replace function to replace substrings in a string and set the number of replacements Jul 25, 2023 am 08:28 AM

Use the strings.Replace function to replace substrings in a string and set the number of replacements. In the Go language, we can use the strings.Replace function to replace substrings in a string. The signature of this function is as follows: funcReplace(s,old,newstring,nint)string where s represents the original string, old represents the substring to be replaced, new represents the replaced substring, and n represents the number of times to replace. Pass below

How to solve php failure to replace empty string How to solve php failure to replace empty string Mar 23, 2023 pm 03:51 PM

In PHP development, string replacement is a very common operation. However, sometimes when performing string replacement, you may encounter the situation of replacing an empty string, and the result is not satisfactory. This article will explore why PHP fails to replace empty strings and provide solutions.

See all articles