This article describes how to replace some characters in a simple php regular expression, then, let me tell you how to replace the URL link address with a regular expression with a specified url address. For more information, see. This article describes how to replace some characters in a simple php regular expression, then, let me tell you how to replace the URL link address with a regular expression with a specified url address. For more information, see.
Script ec (2); script
Related Concepts of PHP Regular Expression replacement:
Preg_replace: search and replace regular expressions.
Mixed preg_replace (mixed pattern, mixed replacement, mixed subject [, int limit]) preg_replace: allows you to replace the regular expression that matches your definition in the string. A simple annotation removal function:
Preg_replace ('[(/*) +. + (*/)] ', '', $ val); this Code removes multiple comments using/* comments */format in PHP and CSS. The three parameters are the regular expression, the string to be replaced, and the target string to be replaced (here the removal function is required, so it is a blank string-> ''). If you want to match sub-rules, you can use $0 to represent all matches, $1, $2, and so on.
Replace spaces-
Example 1:
The Code is as follows: |
|
$ Str = "The quick brown fox jumped over the lazy dog ."; $ Str = preg_replace ('/s/', '-', $ str ); Echo $ str; ?> Output result: The-quick-brown-fox-jumped-over-the-lazy-dog. |
Replace Array
The Code is as follows: |
|
$ Str = "The quick brown fox jumped over the lazy dog ."; $ Patterns [0] = "/quick /"; $ Patterns [1] = "/brown /"; $ Patterns [2] = "/fox /"; $ Replacements [2] = "bear "; $ Replacements [1] = "black "; $ Replacements [0] = "slow "; Print preg_replace ($ patterns, $ replacements, $ str ); /* Output: The bear black slow jumped over the lazy dog. */ Ksort ($ replacements ); Print preg_replace ($ patterns, $ replacements, $ str ); /* Output: The slow black bear jumped over the lazy dog. */ ?> |
Link format: Bootstrap Chinese network,
After collecting the data, we want to change it to the Bootstrap Chinese network.
This involves the keyword of the url link. The sample code is as follows:
The Code is as follows: |
|
$ A = 'bootstrap Chinese network provides the Bootstrap Chinese manual to get started with Bootstrap. Bootstrap is an open source toolkit for front-end development launched by Twitter. It was developed by Twitter designer Mark Otto and Jacob Thornton. It is a CSS/HTML framework. Bootstrap Chinese Network is committed to providing detailed Chinese documents and code examples for Domestic Developers, helping developers master and use this framework. '; $ Lines_string = preg_replace ("(] *> (. + ?)) "," $1 ", $ ); Print_r ($ lines_string ); ?> |