search
Homephp教程php手册php正则表达式解析字符串里的所有URL地址
php正则表达式解析字符串里的所有URL地址 May 25, 2016 pm 04:55 PM
explodeforeachpreg_matchsubstrregular expression

本文章给大家介绍在php正则表达式解析字符串里的所有URL地址实现代码,有需要了解学习的朋友可进入参考。

分享一个同事写的URL正则表达式,缺点不支持中文URL:

 代码如下 复制代码

(http[s]{0,1}|ftp)://[a-zA-Z0-9.-]+.([a-zA-Z]{2,4})(:d+)?(/[a-zA-Z0-9.-~!@#$%^&*+?:_/=]*)?

下面介绍一个可以支持中文正则url

 

 代码如下 复制代码
var regexp = new RegExp("(http[s]{0,1}|ftp)://[a-zA-Z0-9.-]+.([a-zA-Z]{2,4})(:d+)?(/[a-zA-Z0-9.-~!@#$%^&*+?:_/=]*)?", "gi");
var urls = textbox.value.match(regexp) || [];//textbox为文本框
console.log(urls);


实例

 代码如下 复制代码

// $html        = the html on the page
// http://pic2.phprm.com/2013/05/22/$current_url.jpg = the full url that the html came from (only needed for $repath)
// $repath      = converts ../ and / and // urls to full valid urls
function pageLinks($html, $current_url = "", $repath = false){
    preg_match_all("/    $links = array();
    if(isset($matches[2])){
        $links = $matches[2];
    }
    if($repath && count($links) > 0 && strlen($current_url) > 0){
        $pathi      = pathinfo($current_url);
        $dir        = $pathi["dirname"];
        $base       = parse_url($current_url);
        $split_path = explode("/", $dir);
        $url        = "";
        foreach($links as $k => $link){
            if(preg_match("/^../", $link)){
                $total = substr_count($link, "../");
                for($i = 0; $i                     array_pop($split_path);
                }
                $url = implode("/", $split_path) . "/" . str_replace("../", "", $link);
            }elseif(preg_match("/^///", $link)){
                $url = $base["scheme"] . ":" . $link;
            }elseif(preg_match("/^/|^.//", $link)){
                $url = $base["scheme"] . "://" . $base["host"] . $link;
            }elseif(preg_match("/^[a-zA-Z0-9]/", $link)){
                if(preg_match("/^http/", $link)){
                    $url = $link;
                }else{
                    $url       = $dir . "/" . $link;
                }
            }
            $links[$k] = $url;
        }
    }
    return $links;
}
header("content-type: text/plain");
$url = "http://www.phprm.com";
$html = file_get_contents($url);
// Gets links from the page:
print_r(pageLinks($html));

// Gets links from the page and formats them to a full valid url:
print_r(pageLinks($html, $url, true));



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
Java ArrayList遍历时使用foreach和iterator删除元素的区别是什么?Java ArrayList遍历时使用foreach和iterator删除元素的区别是什么?Apr 27, 2023 pm 03:40 PM

一、Iterator和foreach的区别多态差别(foreach底层就是Iterator)Iterator是一个接口类型,他不关心集合或者数组的类型;for和foreach都需要先知道集合的类型,甚至是集合内元素的类型;1.为啥说foreach底层就是Iterator编写的代码:反编译代码:二、foreach与iterator时remove的区别先来看阿里java开发手册但1的时候不会报错,2的时候就会报错(java.util.ConcurrentModificationException)首

php如何判断foreach循环到第几个php如何判断foreach循环到第几个Jul 10, 2023 pm 02:18 PM

​php判断foreach循环到第几个的步骤:1、创建一个“$fruits”的数组;2、创建一个计数器变量“$counter”初始值为0;3、使用“foreach”循环遍历数组,并在循环体中增加计数器变量的值,再输出每个元素和它们的索引;4、在“foreach”循环体外输出计数器变量的值,以确认循环到了第几个元素。

php怎么去除字符串中的所有大写字母php怎么去除字符串中的所有大写字母Sep 26, 2022 pm 07:59 PM

两种去除方法:1、利用preg_replace()执行正则表达式搜索所有大写字母并将其替换为空字符即可,语法“preg_replace('/[A-Z]/','',$str)”。2、利用preg_filter()执行正则表达式搜索所有大写字母并将其替换为空字符即可,语法“preg_filter('/[A-Z]/','',$str)”。

php怎么替换nbsp空格符php怎么替换nbsp空格符Apr 24, 2022 pm 02:55 PM

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\&nbsp\;||\xc2\xa0)/","其他字符",$str)”语句。

PHP返回一个键值翻转后的数组PHP返回一个键值翻转后的数组Mar 21, 2024 pm 02:10 PM

这篇文章将为大家详细讲解有关PHP返回一个键值翻转后的数组,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。PHP键值翻转数组键值翻转是一种对数组进行的操作,它将数组中的键和值进行交换,生成一个新的数组,其中原始键作为值,原始值作为键。实现方法在php中,可以通过以下方法对数组进行键值翻转:array_flip()函数:array_flip()函数专门用于键值翻转操作。它接收一个数组作为参数,并返回一个新的数组,其中键和值已交换。$original_array=[

php怎么利用正则排除字符串中的字符php怎么利用正则排除字符串中的字符Dec 15, 2022 pm 03:30 PM

两种方法:1、用preg_replace(),可执行正则表达式的搜索和替换,只需将字符串中匹配的字符替换为空字符即可,语法“preg_replace(正则, "", $str)”。2、用preg_match_all(),可搜索字符串中所有和正则表达式匹配的结果,会将每次的匹配结果放在一个数组$array中,语法“preg_match_all(正则,$str,$array);”。

php怎么只获取中文字符php怎么只获取中文字符Apr 28, 2022 pm 08:15 PM

php中可用preg_match_all()配合正则表达式过滤字符串,只获取中文字符;语法“preg_match_all("/[\x{4e00}-\x{9fff}]+/u","$str",$arr);”,会将匹配字符存入“$arr”数组中。

javascript怎么正则替换非汉字的字符javascript怎么正则替换非汉字的字符Oct 13, 2022 pm 05:37 PM

在javascript中,可以使用replace()函数配合正则表达式“/[u4e00-u9fa5|,]+/ig”来查找字符串中的所有非汉字字符,并将其替换为其他指定值,语法“字符串对象.replace(/[u4e00-u9fa5|,]+/ig,'指定替换值')”。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

Hot Tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor