PHP regular expression practice: matching image links

WBOY
Release: 2023-06-23 08:40:01
Original
1525 people have browsed it

In the process of web development, we often need to extract image links from articles to display images or store them locally. At this time, regular expressions become an important tool. This article will introduce how to use PHP regular expressions to match image links, and conduct practical exercises through sample code.

1. Matching rules for image links

In highly complex and changeable web pages, the formats of image links vary. The following are some common image link formats:

  1. Relative path format

  1. Absolute path format

  1. Format with class, width, height and other attributes

  1. Format with single or double quotes

  1. Format containing different suffixes



According to the above format, we can summarize a general matching rule, as follows:

/ ?src=['"](.?(?:gif|jpg|jpeg|bmp|png))['"].*?>/i

Where, regular expression Some of the meanings are as follows:

  • : Matches the tag, and the tag may contain other attributes.
  • src=['"](.*?(?:gif|jpg|jpeg|bmp|png))['"]: matches the src attribute value, and the attribute value must be enclosed in double quotes or single quotes Wrapped in quotation marks, multiple suffixes are supported at the same time.
  • .*?: used to match other attributes in thetag.
  • i: Indicates case insensitivity.

2. Use PHP code to match image links

Next, we will use PHP to match image links.

  1. Use the preg_match function to match a single image link

The preg_match function is used to perform regular expression matching on a single string. The following is a PHP code for matching a single image link:

'; $pattern = '//i'; preg_match($pattern, $str, $matches); echo $matches[1]; ?>
Copy after login

The output of the above code is:

../images/picture.jpg

  1. Use the preg_match_all function to match multiple image links

The preg_match_all function is used to perform regular expression matching on a set of strings. The following is a PHP code for matching multiple image links:

    '; $pattern = '//i'; preg_match_all($pattern, $str, $matches); print_r($matches[1]); ?>
Copy after login

The output of the above code is:

Array
(

[0] => ../images/picture.jpg [1] => http://www.example.com/images/picture.jpg [2] => http://www.example.com/images/picture.png [3] => http://www.example.com/images/picture.gif
Copy after login

)

3. Summary

This article introduces how to use PHP regular expressions to match image links, and provides sample code for practical exercises. In actual development, we can modify the matching rules of regular expressions as needed. At the same time, you can also use the matched image link for operations such as image display, downloading or storage.

The above is the detailed content of PHP regular expression practice: matching image links. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!