Hongmu Notes
Home Language Notes PHP regular match all pictures in the article
Language Notes PHP PHP collection

PHP regular match all pictures in the article

PHP regular match all pictures in the article

You can use PHP's regular expression functions. preg_match_all() Matches all image links in the article; the regular expression can be used to match these links based on their format.

Here is an example:

function getImagesFromContent($content) {
    $pattern = '/<img.*?src=[\'"](.*?)[\'"].*?>/i';
    preg_match_all($pattern, $content, $matches);
    return $matches[1];
}

This function takes a string parameter. $contentRepresents the article content; it uses regular expressions. /<img.*?src=[\'"](.*?)[\'"].*?>/i Match all image links within the article..*? Matches zero or more occurrences of any character (including whitespace); use. ? Indicates the non-greedy mode (matches as few characters as possible);[\'"] Indicates matching single or double quotation marks;i Indicates case insensitivity. Matching results will be saved. $matches The second element of the array $matches[1] The function returns this array.

Example usage:

$content = '<p><img src="https://example.com/image1.jpg" /></p>
            <p><img src="https://example.com/image2.jpg" /></p>
            <p><img src="https://example.com/image3.jpg" /></p>';
$images = getImagesFromContent($content);
var_dump($images);

Output result:

array(3) {
  [0]=>
  string(24) "https://example.com/image1.jpg"
  [1]=>
  string(24) "https://example.com/image2.jpg"
  [2]=>
  string(24) "https://example.com/image3.jpg"
}

 

微信赞赏

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

Recommended reading

Printed Poster and Brochure Design Website Template – 1119

Printed Poster and Brochure Design Website Template – 1119 Practical Collection Yiyou template

An EyouCMS website template designed for the printing, poster, and brochure design industries. Its creative visual design style is ideal for showcasing design works, printing processes, and brand case studies. This template helps design and printing companies demonstrate their professional expertise online and attract corporate clients. Template Overview | Installation Instructions | Website Backend: /login.php | Username: admin | Password: admin | Related Articles: Summary of Common Installation Issues for EyouCMS | EyouCMS...
👁 32
Printing poster album design website template 0070

Printing poster album design website template 0070 Practical Collection Yiyou template

This set of eyoucms templates is suitable for printing, poster and album design companies. The design style is creative and visual, and can display design works, printing processes and brand cases. It helps design and printing companies showcase their professional capabilities online and attract corporate customers. Template display Installation instructions Website backend: /login.php Account: admin Password: admin Related articles Summary of common problems in Yiyou CMS installation Yiyou CMS (E…
👁 57
(Adaptive mobile version) Download PBootCMS English-language foreign trade export website source code – 0225

(Adaptive mobile version) Download PBootCMS English-language foreign trade export website source code – 0225 Practical Collection pbootcms Template

This is a comprehensive, adaptive PbootCMS English-language e-commerce export website source code package for mobile devices. The design features an international, minimalist style, making it ideal for export-oriented businesses to create professional English-language official websites. It enables foreign trade companies to showcase their corporate image in global markets at low cost and high efficiency. Template Preview | Installation Instructions | Website Backend: /admin.php | Username: admin | Password: admin | Extraction Password: www.4s5.cn | Related Articles: Pb...
👁 41
PHP remotely obtains the .sql.zip file and then imports it into the database

PHP remotely obtains the .sql.zip file and then imports it into the database Language Notes PHP PHP and mysql

I have published a similar article before "PHP obtains the remote .sql.zip file, then clears the data, and then imports the .sql.zip into the mysql database", but here is another function that is also available for personal testing! 🔒 Hidden content: members can view /** * Download the ZIP compressed file containing the SQL file from the remote URL, decompress the SQL file to the specified directory, and then import the data to M…
👁 214
Responsive Short-Video Live-Streaming E-commerce Marketing Single-Page Website Template – 1120

Responsive Short-Video Live-Streaming E-commerce Marketing Single-Page Website Template – 1120 Practical Collection Yiyou template

This EyouCMS responsive template is ideal for short-form videos, live-streaming e-commerce campaigns, and marketing landing pages. Its trendy design style is perfect for showcasing live-streaming e-commerce case studies, short-form video content, and marketing strategies. It helps MCN agencies or marketing firms attract brand partners online. Template Demo | Installation Instructions | Website Backend: /login.php | Username: admin | Password: admin | Related Articles: Summary of Common Installation Issues for EyouCMS | EyouCMS...
👁 57
Responsive Automation Equipment Technology Company Website Template 0071

Responsive Automation Equipment Technology Company Website Template 0071 Practical Collection Yiyou template

An eyouCMS responsive website template designed for automation equipment technology companies. Its cutting-edge design style is ideal for showcasing automation equipment products, technical solutions, and industrial applications. This template helps automation technology enterprises showcase their brands online and attract manufacturing clients. Template Overview | Installation Instructions | Website Backend: /login.php | Username: admin | Password: admin | Related Articles: Summary of Common Installation Issues for YououCMS | YououCM...
👁 55