Hongmu Notes
Home Language Notes PHP – Extract the content between two specified strings from a web page.
Language Notes PHP PHP collection

PHP – Extract the content between two specified strings from a web page.

PHP – Extract the content between two specified strings from a web page.

To extract the content between two specified strings from a web page, you can use the following regular expression:

$pattern = '/string1(.*?)string2/is';

This regular expression matches. string1string2 The content between them, and use it. (.*?) Capture this content.

Here is an example code:

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

// 截取指定字符串之间的内容
$string1 = 'start';
$string2 = 'end';
$pattern = '/'.$string1.'(.*?)'.$string2.'/is';
if (preg_match($pattern, $html, $matches)) {
    // 输出匹配结果
    echo $matches[1];
} else {
    echo '没有找到指定字符串之间的内容';
}

In the above code, we use preg_match() The function matches the content between specified strings and returns the matching result. If no matching content is found between the specified strings, it returns the message "No content found between the specified strings."

微信赞赏

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 …
👁 180

Recommended reading

(PC + WAP) pbootCMS – High-end Hotpot Base Sauce and Food Seasoning Website Template; Download Marketing-Oriented Catering and Food Service Website Source Code – 0660

(PC + WAP) pbootCMS – High-end Hotpot Base Sauce and Food Seasoning Website Template; Download Marketing-Oriented Catering and Food Service Website Source Code – 0660 Practical Collection pbootcms Template

A PbootCMS-based marketing template designed for the premium hot pot base and food seasoning industry, compatible with both PC and WAP devices. Its appealing and appetizing design is ideal for showcasing culinary products and condiments. This template integrates functional modules—including product display, brand storytelling, and franchise recruitment—effectively helping food businesses enhance their online brand image and drive the growth of their franchise operations. Template Display | Installation Instructions | Website Backend: /admin.php | Username: ad...
👁 63
Responsive Entrepreneurial Wealth Management Website Template 1168

Responsive Entrepreneurial Wealth Management Website Template 1168 Practical Collection Yiyou template

This EyouCMS responsive template is ideal for the entrepreneurship and wealth management industries, featuring a professional financial design style perfect for showcasing entrepreneurial projects, wealth management services, investment products, and success stories. It helps wealth management institutions attract entrepreneurs and high-net-worth clients online. Template Display | Installation Instructions | Website Backend: /login.php | Username: admin | Password: admin | Related Articles: Summary of Common Installation Issues for EyouCMS | EyouCMS...
👁 44
pbootCMS Manual Payment Plugin

pbootCMS Manual Payment Plugin Practical Collection pb tool

Alipay Face-to-Face Payment Plugin – includes a video tutorial! To enable the Alipay Face-to-Face Payment feature in PBootCMS, simply follow the video tutorial for configuration after successful application. Installation Guide: Step 1: Copy the files to the website's root directory; these are all new files and will not modify the existing PBootCMS system. Step 2: Apply for an Alipay Face-to-Face Payment API key (search for it online using Baidu); enter the key as follows: \apps\home\controller\Pay...
👁 862
Interior Decoration and Renovation Website Templates 0315

Interior Decoration and Renovation Website Templates 0315 Practical Collection Yiyou template

This EyouCMS template is ideal for the interior decoration and renovation industry. Its modern, professional design style is perfect for showcasing renovation project examples, construction techniques, material displays, and service workflows. It helps renovation and decoration companies attract clients online and secure new projects. Template Display | Installation Instructions | Website Backend: /login.php | Username: admin | Password: admin | Related Articles: Summary of Common Installation Issues for EyouCMS | EyouCMS...
👁 40
(Adaptive Mobile Version) pbootCMS Responsive Vacuum Pump and Water Pump Equipment Corporate Website Template – Download Source Code for Vacuum Pump and Mechanical Equipment Websites – 0661

(Adaptive Mobile Version) pbootCMS Responsive Vacuum Pump and Water Pump Equipment Corporate Website Template – Download Source Code for Vacuum Pump and Mechanical Equipment Websites – 0661 Practical Collection pbootcms Template

A responsive vacuum pump equipment and automatic vacuum pump PbootCMS corporate website template, compatible with both PC and WAP devices. The professional, tech-oriented design is ideal for showcasing pump products, technical specifications, and industrial applications. It helps pump manufacturing enterprises demonstrate their product capabilities online and attract industrial customers. Template Display | Installation Instructions | Website Backend: /admin.php | Username: admin | Password: admin | Extraction Password: www....
👁 59