Home >Backend Development >PHP Tutorial >PHP regular matching method reference for image paths

PHP regular matching method reference for image paths

WBOY
WBOYOriginal
2016-07-25 08:58:031619browse
This article details the method of using regular expressions to match image paths in PHP. Friends in need can refer to it.

php regular matching image path:

Copy code Code example: /PHP regular matching method reference for image paths"'s]*)/i

To use kindeditor to save an article, you need to take out the address of the Nth picture as the logo picture of the article, save the article code (html of the content) to a field in the database, and then save the image address to another field. The above address directly obtains the value of the src attribute in the img tag. If the image can be found when accessing the path on the php page using this regular pattern, you can use it directly. If not, you can use preg_match_all to save all addresses to an array first, and then process the path, such as getting the file name (excluding the path part), and then Reorganize the url and delete the image.

Example:

Copy code Code example: preg_match_all("/PHP regular matching method reference for image paths"'s]*)/i",str_ireplace("\","",$content),$arr);

The content part has been escaped by PHP. You need to remove it first, str_ireplace("\","",$content), and then save the matching content to the $arr array (two-dimensional). $arr[1] is the array that stores the path.

Let’s talk about php regular extraction of image address

Refer to the previous article, I will briefly discuss the method of extracting the image address in PHP. Extracting the image address in src= is not enough, because there is no guarantee that the address must be an absolute address or a complete address. What if it is relative? If the address is something like: albums/Candids/thumb_P1050338.jpg /content/media/touts/5271608/5271654/15320982 How should this be done?

Sometimes http://example1.com/ needs to be added in front of these addresses, and some even need to add http://example1.com/example2/.../, which is too unrealistic.

After thinking about it, it turns out: http://example.com/ and http://example.com////// are the same! http://img3.jbxue.com/pics/nav/lg_main_a6.png and http://img3.jbxue.com////pics////nav///lg_main_a6.png Therefore, if you want to forcibly add a prefix to the two relative addresses mentioned at the beginning to restore them to absolute addresses, it doesn't matter whether there is a "/" in front of it, just add a "/". "Well, the display will still be normal if there is one more, but there is one less "/".



Statement:
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