Home >Backend Development >PHP Tutorial >How to use preg_replace() function in PHP for regular expression replacement

How to use preg_replace() function in PHP for regular expression replacement

WBOY
WBOYOriginal
2023-11-18 17:50:521516browse

How to use preg_replace() function in PHP for regular expression replacement

How to use the preg_replace() function in PHP for regular expression replacement

Regular expression is a powerful pattern matching tool. In PHP, use preg_replace The () function can implement regular expression replacement for strings. This article will introduce how to use the preg_replace() function for regular expression replacement and provide specific code examples.

The syntax of the preg_replace() function is as follows:
string preg_replace ( mixed $pattern , mixed $replacement , mixed $subject [, int $limit = -1 [, int &$count ]] )

Among them:

  • pattern: regular expression pattern, used to match strings.
  • replacement: replacement content.
  • subject: The string to be replaced.
  • limit: Optional parameter, used to specify the number of replacements. The default is -1, which means replacing all matching items.
  • count: Optional parameter, used to store the number of substitutions.

The following uses several practical examples to demonstrate the use of the preg_replace() function.

  1. Replace numbers:
    We can use regular expressions to replace numbers in a string. The following example replaces all numbers in the string with "#".
$str = "Today is 2022/12/31.";
$newStr = preg_replace("/d+/", "#", $str);
echo $newStr;

Output result:

Today is #/#/#.
  1. Replace email address:
    We can use regular expressions to match the email address and replace it with "@.com". The following example demonstrates how to replace email addresses in a string.
$str = "My email is test@example.com.";
$newStr = preg_replace("/[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+.[A-Za-z]{2,}/", "***@***.com", $str);
echo $newStr;

Output result:

My email is ***@***.com.
  1. Replace HTML tag:
    We can use regular expressions to match HTML tags and replace them with empty strings, thus Realize the effect of deleting HTML tags. The following example demonstrates how to remove HTML tags from a string.
$html = "<p>Hello, <b>world</b>!</p>";
$cleanHtml = preg_replace("/<[^>]*>/", "", $html);
echo $cleanHtml;

Output result:

Hello, world!

It should be noted that the preg_replace() function performs pattern matching through regular expressions, so the correct writing of the pattern is crucial to the effect of replacement. . When writing regular expressions, you can use various special characters, quantifiers, selectors, etc. to build more flexible pattern matching rules.

To sum up, the preg_replace() function is a powerful function in PHP for regular expression replacement. By mastering the syntax of regular expressions and the use of the preg_replace() function, you can replace various patterns in strings.

The above is the detailed content of How to use preg_replace() function in PHP for regular expression replacement. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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

Related articles

See more