php using regular expression to get URL in string
After writing a Q&A system today and going online, I found that many people sent links. Since the business department asked us to filter out the website address, I will share with you a function to extract the string url address. The code is as follows:
$str ='本文实例讲述了php匹配字符串里所有URL地址的方法。http://www.manongjc.com 分享给大家供大家参考'; preg_match_all("/http:[\/]{2}[a-z]+[.]{1}[a-z\d\-]+[.]{1}[a-z\d]*[\/]*[A-Za-z\d]*[\/]*[A-Za-z\d]*/",$str,$array2); print_r($array2);
The running result is:
( [0] => Array ( [0] => http://www.manongjc.com ) )
The preg_match_all function is mainly used here. The specific usage of this function is as follows:
preg_match_all - perform global regularization Expression matching
Syntax:
int preg_match_all ( string pattern, string subject, array matches [, int flags] )
Search in the subject for all content that matches the regular expression given by pattern and put the results in the order specified by flags matches.
After searching for the first match, subsequent searches start from the end of the previous match.
Pay special attention to PREG_PATTERN_ORDER and PREG_SET_ORDER
flags can be a combination of the following flags (note that it makes no sense to use PREG_PATTERN_ORDER and PREG_SET_ORDER together):
If you use PREG_PATTERN_ORDER
Sort the results so that $matches[0] is an array of all pattern matches, $matches[1] is an array of strings matched by the subpattern in the first bracket, and so on. (That is, $matches[0] [0] is the second item in all pattern matches, $matches[0] [1] is the second item in all pattern matches, $matches[1] [0] is the match for each item. The first item in the brackets, $matches[1] [0] matches the second item in each bracket)
<?php preg_match_all ("|<[^>]+>(.*)</[^>]+>|U","<b>example: </b><div align=left>this is a test</div>",$out, PREG_PATTERN_ORDER); /* http://www.manongjc.com/article/1591.html */ print $out[0][0].", ".$out[0][1]."\n"; print $out[1][0].", ".$out[1][1]."\n"; ?>
Output result:
<b>example: </b>, <div align=left>this is a test</div> example: , this is a test
Update For related articles on using regular expressions to obtain the URL in a string, please pay attention to the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Alipay PHP...

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

Article discusses essential security features in frameworks to protect against vulnerabilities, including input validation, authentication, and regular updates.

The article discusses adding custom functionality to frameworks, focusing on understanding architecture, identifying extension points, and best practices for integration and debugging.

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...
