PHP function introduction: str_replace() function

王林
Release: 2023-11-03 18:10:01
Original
1037 people have browsed it

PHP function introduction: str_replace() function

PHP function introduction: str_replace() function, specific code examples are required

PHP is a popular server-side scripting language that is often used for website development. In PHP, there are a large number of functions that can be used to extend the functionality of the website. One of them is the str_replace() function, which is used to replace substrings in a string. This article will introduce the usage of str_replace() function and provide some specific code examples.

The syntax of the str_replace() function is as follows:

str_replace($search, $replace, $subject)
Copy after login

Among them, $search represents the substring to be replaced, $replace represents the replaced substring, and $subject represents the search and replacement. the original string. All three parameters can be strings or arrays, and multiple substrings can be replaced at the same time.

The following is a simple example to replace "world" in the string with "PHP":

$oldstr = "Hello, world!"; $newstr = str_replace("world", "PHP", $oldstr); echo $newstr;
Copy after login

The output is:

Hello, PHP!
Copy after login

In addition to the words, str_replace The () function can also be used to replace other strings, such as punctuation marks, numbers, etc. The following is an example of using array replacement:

$oldstr = "Hello, my name is John."; $search = array(",", "John"); $replace = array(";", "Peter"); $newstr = str_replace($search, $replace, $oldstr); echo $newstr;
Copy after login

The output is:

Hello; my name is Peter.
Copy after login

If you want to replace all matches in a string, you can use the preg_replace() function.

The str_replace() function can also be used to process URLs and HTML tags. For example, you can protect the security of your website by replacing the URL:

$url = "http://www.example.com/index.php?id=1"; $newurl = str_replace("example.com", "mywebsite.com", $url); echo $newurl;
Copy after login

The output is:

http://www.mywebsite.com/index.php?id=1
Copy after login

Similarly, you can also use the str_replace() function to replace HTML tags:

$html = "

Hello, world!

"; $newhtml = str_replace(array("", "", "", ""), array("", "", "", ""), $html); echo $newhtml;
Copy after login

The output result is:

Hello, world!

Copy after login

In short, the str_replace() function is a very useful PHP function that can be used to replace any substring in a string. When developing a website, you often need to use this function. Hopefully the code examples provided in this article will help you better understand the use of this function.

The above is the detailed content of PHP function introduction: str_replace() function. For more information, please follow other related articles on the PHP Chinese website!

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
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!