How to replace all matching strings in php

王林
Release: 2023-03-05 21:38:02
Original
3072 people have browsed it

How to replace all qualified strings in php: You can use the preg_replace() function to achieve this. The preg_replace() function is used to perform a regular expression search and replacement.

How to replace all matching strings in php

php provides a function preg_replace(), which can achieve our needs very well. Let's talk about the usage of this function.

(Recommended tutorial: php video tutorial)

preg_replace function performs a regular expression search and replacement.

Full syntax:

mixed preg_replace(mixed $pattern, mixed $replacement, mixed $subject[,int $limit=-1[,int &$count]])
Copy after login

Return value:

If subject is an array, preg_replace() returns an array, otherwise it returns a string.

If a match is found, the replaced subject is returned, otherwise the unchanged subject is returned. If an error occurs, NULL is returned.

Example:

<?php
$str = &#39;phpcn c   n&#39;;
$str = preg_replace(&#39;/\s+/&#39;, &#39;&#39;, $str);
echo $str;
?>
Copy after login

Related recommendations:php training

The above is the detailed content of How to replace all matching strings in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!