迷上了正则,不断尝试着新花招,首先感谢TNA 的非完全输出RSS,然后再次感谢SH的强迫性学习。没有TNA,我不会去看正则,更不知道世界上有种这么牛的表达式;不是SH的死活说他不懂不知道,我也不会硬着头皮去琢磨,去改进。达到同一个目的,正则的表达方式可以不唯一,没有做不到,只有你没想到。可以这样说吧,正则就是玩设定规律,我大爱这种东西。没有比设定规律筛选东西更让我兴奋、感到awesome的了。
分享一下在php环境下使用正则提取图片地址的一些小心得:
图片网址规范的html代码无非就是
复制代码 代码如下:
囧1和囧2是非必需的,若要通过XHTML认证囧4、囧5、囧6必不可少,囧3是核心内容,当然就不能少了。
就正则谈正则的话,我写出的最短匹配是
复制代码 代码如下:
(?
不过,这条在php里不行,会出现:
Warning: preg_match_all() [function.preg-match-all]: Compilation failed: lookbehind assertion is not fixed length at offset *** in ***
纠结了很久,都不行,原因何在呢?试了很多次,终于发现问题在(?
所以
复制代码 代码如下:
(?
或
复制代码 代码如下:
(?
可能可以,但不保证100%没问题。
你也许会问,单纯
复制代码 代码如下:
(?
不行吗?通常情况,可以,但,搜索过页面的盆友应该知道,除了图片地址用src开头以外,javascript地址也用src开头!而且,太多神通广大的不可预知因素隐含其中,于是这个貌似很简短完美的写法就行不通了。
你又或许会问,聪明简短的不行,我把图片的后缀列出来,总该可以了吧,如
复制代码 代码如下:
(?
的确,这个写法实在是很老实,不过,你见过没有后缀的图片?wwe.com 有很多这种例子呢
RAW http://us.wwe.com/content/media/images/Headers/15559182
SmackDown http://us.wwe.com/content/media/images/Headers/15854138
NXT http://us.wwe.com/content/media/images/Headers/15929136
Superstars http://us.wwe.com/content/media/images/Headers/15815850
上面的网址都是图片,但都没有传统后缀,你老实也没用,还是不能获取到它们。
怎么办呢?还可以这样
复制代码 代码如下:
和上面的表达式不同,这次的结果中array[0]的内容不是我们想要的,我们要的图片地址在array[2]里。为什么呢?因为我们用了2个 (.*?),每个“()”的东西会自动存在一个组里,而array[0]代表结果的汇总,array[1]包含了img和src里的所有东西,array[2]才轮到我们想要的图片地址。这种匹配方法,既能匹配有传统后缀的图片,也能匹配一些无后缀的图片文件,同时又不会杀错其它src=文件。个人感觉还是不错的,呵呵。当然了,如果你还有更好的建议,请马上留言,全球人民都会感谢你!
你到底要什么样的图片,是固定格式还是其它?得具体情况具体分析呢。
我的建议是:
如果你要的图片地址的格式是img空格src=的,请使用:(?
否则,请使用
再谈php正则提取图片地址
前天写了小谈php正则提取图片地址 ,但其实,提取src=里面的图片地址还不足够,因为不能保证那个地址一定是绝对地址,完全的地址,如果那是相对的呢?如果地址诸如:
albums/Candids/thumb_P1050338.jpg
/content/media/touts/5271608/5271654/15320982
那该如何是好?
有时在这些地址前面需要加http://example1.com/ ,有些甚至要加http://example1.com/example2/.../ 于是,要写出出一种法则符合所有要求,简直是天方夜谭。只能见机行事对症下药。有时,需要从前面动刀,有时需要从后面砍断。
今天,我惊讶地知道了一个道理,原来http://example.com/ 和http://example.com////// 是一样的!
http://img3.douban.com/pics/nav/lg_main_a6.png
和
http://img3.douban.com////pics////nav///lg_main_a6.png
最终你都能到达
于是,对于一开始提到的两个相对地址如果要强行加入某前缀恢复成绝对地址的话,也不管前面有没有“/”,只管加一个“/”就好,“有杀错,没放过” 嘛,多一个显示仍会正常,但少一个“/”,嘿嘿,你就别想成功了。开始的时候我还没意识到这种东西,复制了一大段代码,把一样的东西硬生生弄两份,一份加 “./.”,一份不加。我这个火星来的,浪费时间了。
放出2个地址,公测一下网页获取图片的情况:
针对任何网页,需要登入的除外:http://xyark.serw5.com/img.php
针对Coppermine Photo Gallery 系统:http://xyark.serw5.com/g.php (如果你认为弹出原图的js页面也需要的话,我只好囧你了)
普页是个对抓取任何图片的尝试,系统专页是为了展示什么叫做具体情况具体分析。试过的童鞋会知道,普页对某些使用Coppermine Photo Gallery系统的网站是行不通的,原因何在?就是那个前缀搞的鬼!但系统专页就能很好地避开了这个问题。
如果大家在测试时发现任何bug,欢迎留言告知。请低调测试,谢谢合作。
注:以上话题纯粹出于就正则谈正则,光技术谈技术,不可作非正当用途。若非正当使用而引发任何杯具、餐具本人概不负责。
转载时请以超链接形式标明文章原始出处和作者信息及本声明
http://www.blogbus.com/xrspook-logs/85330456.html

PHP is used to build dynamic websites, and its core functions include: 1. Generate dynamic content and generate web pages in real time by connecting with the database; 2. Process user interaction and form submissions, verify inputs and respond to operations; 3. Manage sessions and user authentication to provide a personalized experience; 4. Optimize performance and follow best practices to improve website efficiency and security.

PHP uses MySQLi and PDO extensions to interact in database operations and server-side logic processing, and processes server-side logic through functions such as session management. 1) Use MySQLi or PDO to connect to the database and execute SQL queries. 2) Handle HTTP requests and user status through session management and other functions. 3) Use transactions to ensure the atomicity of database operations. 4) Prevent SQL injection, use exception handling and closing connections for debugging. 5) Optimize performance through indexing and cache, write highly readable code and perform error handling.

Using preprocessing statements and PDO in PHP can effectively prevent SQL injection attacks. 1) Use PDO to connect to the database and set the error mode. 2) Create preprocessing statements through the prepare method and pass data using placeholders and execute methods. 3) Process query results and ensure the security and performance of the code.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP makes it easy to create interactive web content. 1) Dynamically generate content by embedding HTML and display it in real time based on user input or database data. 2) Process form submission and generate dynamic output to ensure that htmlspecialchars is used to prevent XSS. 3) Use MySQL to create a user registration system, and use password_hash and preprocessing statements to enhance security. Mastering these techniques will improve the efficiency of web development.

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Zend Studio 13.0.1
Powerful PHP integrated development environment

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.