Regular url function preg_match_PHP tutorial in php

WBOY
Release: 2016-07-13 16:59:09
Original
1017 people have browsed it

The following uses preg_match to give two examples: one is to obtain the content of the specified area, and the other is to obtain the url address in the content.

The following uses preg_match to give two examples: one is to obtain the content of the specified area, and the other is to obtain the url address in the content.
*/
$string="example:

this is a test
"; //Define string
$pattern="|<[^>]+>(.*)]+>|u"; //Define regular expression pattern
/*The meaning of this matching pattern is: starting with "<" followed by 1 to more characters that are not ">" and ending content; ".*" in the sub-pattern represents 0 to more any characters. In addition, it starts with "<" followed by one or more characters that are not ">" plus the ">" symbol. */
preg_match_all($pattern,$string,$out,preg_pattern_order); //Perform preg_mathc_all processing
echo $out[0][0];
echo ",";
echo $out[0][1];
echo "

";
echo $out[1][0];
echo ",";
echo $out[1][1];

//Example 2

//The following code is used to extract the domain name
$http="http://www.bKjia.c0m"; //Define URL
preg_match("/^(http://)?([^/]+)/i",$http,$matches); //Regular expression matching
$host=$matches[2]; //Assign the result array elements to the variable
preg_match("/[^./]+.[^./]+$/",$host,$matches); //Regular expression matching
echo "domain name is:{$matches[0]}n"; //Output result domain name

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/631356.htmlTechArticleThe following uses preg_match to give two examples. One is to obtain the content of the specified area, and the other is to obtain the url address in the content. Below are two examples using preg_match. One is to obtain the specified area...
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