Hongmu Notes
Home Language Notes PHP regular matches all href links
Language Notes PHP PHP collection

PHP regular matches all href links

PHP regular matches all href links

In HTML, links are typically written as <a> The tag appears in this form. href The attribute specifies the link address. You can use regular expressions to match it. <a> Tag and extract its contents. href Attribute value. Here is an example regular expression:

$pattern = '/<a\s[^>]*href=([\'"])(.*?)\1[^>]*>/i';

This regular expression matches all. <a> Tag and extract its contents. href Attribute value. Here,([\'"]) Indicates matching quotation marks (single or double quotation marks),\1 Indicates the reference to the first captured group (i.e., the matched quotation mark).(.*?) Represents a non-greedy match of any character until the next quotation mark is encountered.

Here is an example code snippet:

// 获取目标 URL 的 HTML 内容
$html = file_get_contents('https://example.com');

// 匹配所有链接
$pattern = '/<a\s[^>]*href=([\'"])(.*?)\1[^>]*>/i';
if (preg_match_all($pattern, $html, $matches)) {
    // 提取链接地址
    $links = $matches[2];
    foreach ($links as $link) {
        echo $link . '<br>';
    }
} else {
    echo '没有找到链接';
}

In the above code, we first retrieve the HTML content of the target URL, then use a regular expression to match all links within that content. If the match is successful, we extract the link addresses from the match results and output them; otherwise, we output "No links found."

微信赞赏

WeChat

支付宝赞赏

Alipay

✍️ Author: Hong Mu

webmaster · Thanks for reading, stay tuned for more exciting content

Author homepage View home page →

Related articles

PHP cast type

PHP cast type Language Notes PHP PHP collection PHP and mysql

Get the data type 1. If you want to check the value and type of an expression, use var_dump(). 2. If you just want to get an easy-to-read type expression for debugging, use gettype(). 3. To check a certain type, do not use gettype(), but use the is_type() function. Converting Strings to Numbers When a string is evaluated as a number, the result is determined according to the following rules...
👁 232
PHP capture website source code and json parsing

PHP capture website source code and json parsing Language Notes PHP PHP collection

Recently, I wanted to collect the names of some resource websites, so I found a fun one when I collected it. The opposite website was loaded by ajax technology, so I found the api, so I just wrote the code directly to put it in the warehouse. The code is backed up here and will be used in the future. & lt; ? php $date = file_get_contents(' Website address ' ); $date = json_deco…
👁 136
PHP special character escaping and restoration

PHP special character escaping and restoration Language Notes PHP PHP collection PHP and mysql

Escape character is a special character constant. Escape characters are backslashed " & quot; At the beginning, followed by one or more characters. The escaped character has a specific meaning, which is different from the original meaning of the character, so it is called "escaped" character. The use of escape characters 1: turn ordinary characters into special purposes, such as back key and enter key. 2. Used to convert a character with special meaning back to its original meaning. 3. Before data is written into the database, escape characters (function …
👁 179

Recommended reading

(Adaptive mobile version) HTML5 Blue Mechanical & Electrical Equipment PBootCMS Website Template – Download source code for generator maintenance websites – 0664

(Adaptive mobile version) HTML5 Blue Mechanical & Electrical Equipment PBootCMS Website Template – Download source code for generator maintenance websites – 0664 Practical Collection pbootcms Template

A blue-themed PbootCMS website template designed for power equipment and generator maintenance websites, compatible with both PC and WAP devices. Its professional and reliable design is ideal for showcasing power equipment, maintenance services, and project cases. This template helps power equipment companies demonstrate their technical expertise online and attract both industrial and commercial clients. Template Overview | Installation Instructions | Website Backend: /admin.php | Username: admin | Password: admin | Extraction Password: www....
👁 54
Responsive Medical Testing Equipment Website Template 0320

Responsive Medical Testing Equipment Website Template 0320 Practical Collection Yiyou template

An eyouCMS responsive website template designed for the medical testing equipment industry. Its professional, tech-oriented design is ideal for showcasing testing equipment products, medical devices, technical specifications, and clinical applications. This template helps medical device companies showcase their products online and attract clients from hospitals and testing institutions. Template Overview | Installation Instructions | Website Backend: /login.php | Username: admin | Password: admin | Related Articles: Common Questions About Installing eyouCMS...
👁 36
Responsive Financial Planning Service Management Website Template 1175

Responsive Financial Planning Service Management Website Template 1175 Practical Collection Yiyou template

An EyouCMS responsive website template designed for financial wealth management service management. Its professional financial design enables effective presentation of wealth management products, investment services, wealth management solutions, and success stories. This template helps financial institutions showcase their brand online and attract investors. Template Overview | Installation Instructions | Website Backend: /login.php | Username: admin | Password: admin | Related Articles: Summary of Common Installation Issues for EyouCMS | EyouCMS (E...
👁 41
(PC+WAP) antique auction exhibition pbootcms website template antique pawn website source code download 0665

(PC+WAP) antique auction exhibition pbootcms website template antique pawn website source code download 0665 Practical Collection pbootcms Template

A PbootCMS template specially designed for antique auction, antique pawn and exhibition websites, supporting PC and WAP. The design style is calm and heavy, highlighting the historical and artistic value. The page layout is suitable for displaying rare collections, auction previews and exhibition information, and helps art institutions and pawn shops establish a high-end and professional brand image online. Template display Installation instructions Website backend:/admin.php Account: admin Password:…
👁 62
Responsive Healthcare & Beauty Examination Website – 0321

Responsive Healthcare & Beauty Examination Website – 0321 Practical Collection Yiyou template

This EyouCMS responsive template is ideal for the healthcare, medical check-up, and beauty industries. Its professional and clean design effectively showcases medical services, health check-up packages, beauty treatments, expert teams, and success stories. It helps medical and beauty institutions attract customers online and enhance their brand professionalism. Template Display | Installation Instructions | Website Backend: /login.php | Username: admin | Password: admin | Related Articles: Common Questions About EyouCMS Installation...
👁 30