Replace the URL url address with the php regular expression with the specified URL-PHP source code

Source: Internet
Author: User
Tags php regular expression
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 );
?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.