search
HomeBackend DevelopmentPHP Tutorial一个字串筛选问题

本帖最后由 ruiman 于 2014-02-08 11:04:14 编辑

PHP 字符串 正则表达式

才在学习PHP,现在遇到一个$contents变量里面的内容如下 document.write('
一个字串筛选问题 width="60"  style="max-width:90%" align="absbottom"  
border="0">
    target="_blank">教育新闻

href="http://news.ycwb.com/2014-02/08/content_6034510.htm" 
target="_blank">川大教授张兴栋当选美国国家工程院外籍院士  金羊网 06:29
href="http://news.xinhuanet.com/edu/2014-02/08/c_126099001.htm" 
target="_blank">北京推一本高招计划 定向投放生态涵养区  新华网 09:11
target="_blank">肇庆:主要景区迎客221万人次 光明网 08:29
target="_blank">把对故乡眷恋藏心里 春节后娃娃随父母出发 组图  新华网 08:47
href="http://news.xinhuanet.com/edu/2014-02/08/c_119240720.htm" 
target="_blank">促进高校毕业生就业有哪些新的改革举措  新华网 10:18
target="_blank">借力“适应性学习” Knewton演绎在线教育变革 搜狐 09:55
href="http://www.sd.xinhuanet.com/news/2014-02/08/c_119235535.htm" target="_blank">山东为自费留学中介上紧箍 
严禁到中小学招生  新华网山东频道 08:03
target="_blank">西汉“学术超男”策划“诗经秀”取功名 人民网 07:04
target="_blank">李鸿章哥哥摆架子被下级痛骂 人民网 07:04
target="_blank">江苏金坛教育系统工作“贴地行走”摸准基层脉 中国教育新闻网 08:50

target="_blank">更多>>
');
现在想把里面的这样的内容  href="http://news.ycwb.com/2014-02/08/content_6034510.htm" 
 target="_blank">川大教授张兴栋当选美国国家工程院外籍院士  筛几条出来,请问应如何操作。

回复讨论(解决方案)

"筛几条" 是指哪几条?这个标准怎么定义? 

$str="content";preg_match_all("#<a\s*[\n|\r]\s*href=.*?target=\"_blank\">.*?</a>#is", $str, $matches);print_r($matches);

$str="content";preg_match_all("#<a\s*[\n|\r]\s*href=.*?\s*[\n|\r]\s*target=\"_blank\">.*?</a>#i", $str, $matches);print_r($matches);

$s =<<< TXTdocument.write('<DIV style="margin-bottom: 10px;"><A href="http://news.baidu.com/" target="_blank"><img  src="/static/imghwm/default1.png"  data-src="Other%20web_files/slogo-news.gif"  class="lazy"     style="max-width:90%"  style="max-width:90%" align="absbottom"  border="0" alt="一个字串筛选问题" ></A>   <A class="blk" href="http://news.baidu.com/n?cmd=1&class=edunews&pn=1" target="_blank">教育新闻</A></DIV><DIV class="baidu" style="width: 100%; text-align: left; table-layout: fixed; -ms-word-wrap: break-word;"><A href="http://news.ycwb.com/2014-02/08/content_6034510.htm" target="_blank">川大教授张兴栋当选美国国家工程院外籍院士 </A> <SPAN>金羊网 06:29</SPAN><BR><A href="http://news.xinhuanet.com/edu/2014-02/08/c_126099001.htm" target="_blank">北京推一本高招计划 定向投放生态涵养区 </A> <SPAN>新华网 09:11</SPAN><BR><A href="http://news.gmw.cn/newspaper/2014-02/08/content_2849438.htm" target="_blank">肇庆:主要景区迎客221万人次</A> <SPAN>光明网 08:29</SPAN><BR><A href="http://news.xinhuanet.com/edu/2014-02/08/c_126098505.htm" target="_blank">把对故乡眷恋藏心里 春节后娃娃随父母出发 组图 </A> <SPAN>新华网 08:47</SPAN><BR><A href="http://news.xinhuanet.com/edu/2014-02/08/c_119240720.htm" target="_blank">促进高校毕业生就业有哪些新的改革举措 </A> <SPAN>新华网 10:18</SPAN><BR><A href="http://learning.sohu.com/20140208/n394601900.shtml" target="_blank">借力“适应性学习” Knewton演绎在线教育变革</A> <SPAN>搜狐 09:55</SPAN><BR><A href="http://www.sd.xinhuanet.com/news/2014-02/08/c_119235535.htm" target="_blank">山东为自费留学中介上紧箍 严禁到中小学招生 </A> <SPAN>新华网山东频道 08:03</SPAN><BR><A href="http://edu.people.com.cn/n/2014/0208/c1053-24295188.html" target="_blank">西汉“学术超男”策划“诗经秀”取功名</A> <SPAN>人民网 07:04</SPAN><BR><A href="http://edu.people.com.cn/n/2014/0208/c1053-24295191.html" target="_blank">李鸿章哥哥摆架子被下级痛骂</A> <SPAN>人民网 07:04</SPAN><BR><A href="http://www.jyb.cn/basc/xw/201402/t20140208_569160.html" target="_blank">江苏金坛教育系统工作“贴地行走”摸准基层脉</A> <SPAN>中国教育新闻网 08:50</SPAN><BR><DIV style="font-size: 12px; margin-top: 5px;"><A class="more" href="http://news.baidu.com/n?cmd=1&class=edunews&pn=1" target="_blank">更多>></A></DIV></DIV>');TXT;//全部 A 标记preg_match_all('#<a.+</a>#isU', $s, $r);print_r($r[0]);//仅文字标题的 A 标记preg_match_all('#<a[^<]+</a>#isU', $s, $r);print_r($r[0]);//仅包含指定词汇的 A 标记preg_match_all('#<a[^<]+院士.*</a>#isU', $s, $r);print_r($r[0]);
Array(    [0] => <A href="http://news.baidu.com/" target="_blank"><img  src="/static/imghwm/default1.png"  data-src="Other%20web_files/slogo-news.gif"  class="lazy"     style="max-width:90%"  style="max-width:90%" align="absbottom"  border="0" alt="一个字串筛选问题" ></A>    [1] => <A class="blk" href="http://news.baidu.com/n?cmd=1&class=edunews&pn=1" target="_blank">教育新闻</A>    [2] => <A href="http://news.ycwb.com/2014-02/08/content_6034510.htm" target="_blank">川大教授张兴栋当选美国国家工程院外籍院士 </A>    [3] => <A href="http://news.xinhuanet.com/edu/2014-02/08/c_126099001.htm" target="_blank">北京推一本高招计划 定向投放生态涵养区 </A>    [4] => <A href="http://news.gmw.cn/newspaper/2014-02/08/content_2849438.htm" target="_blank">肇庆:主要景区迎客221万人次</A>    [5] => <A href="http://news.xinhuanet.com/edu/2014-02/08/c_126098505.htm" target="_blank">把对故乡眷恋藏心里 春节后娃娃随父母出发 组图 </A>    [6] => <A href="http://news.xinhuanet.com/edu/2014-02/08/c_119240720.htm" target="_blank">促进高校毕业生就业有哪些新的改革举措 </A>    [7] => <A href="http://learning.sohu.com/20140208/n394601900.shtml" target="_blank">借力“适应性学习” Knewton演绎在线教育变革</A>    [8] => <A href="http://www.sd.xinhuanet.com/news/2014-02/08/c_119235535.htm" target="_blank">山东为自费留学中介上紧箍 严禁到中小学招生 </A>    [9] => <A href="http://edu.people.com.cn/n/2014/0208/c1053-24295188.html" target="_blank">西汉“学术超男”策划“诗经秀”取功名</A>    [10] => <A href="http://edu.people.com.cn/n/2014/0208/c1053-24295191.html" target="_blank">李鸿章哥哥摆架子被下级痛骂</A>    [11] => <A href="http://www.jyb.cn/basc/xw/201402/t20140208_569160.html" target="_blank">江苏金坛教育系统工作“贴地行走”摸准基层脉</A>    [12] => <A class="more" href="http://news.baidu.com/n?cmd=1&class=edunews&pn=1" target="_blank">更多>></A>)Array(    [0] => <A class="blk" href="http://news.baidu.com/n?cmd=1&class=edunews&pn=1" target="_blank">教育新闻</A>    [1] => <A href="http://news.ycwb.com/2014-02/08/content_6034510.htm" target="_blank">川大教授张兴栋当选美国国家工程院外籍院士 </A>    [2] => <A href="http://news.xinhuanet.com/edu/2014-02/08/c_126099001.htm" target="_blank">北京推一本高招计划 定向投放生态涵养区 </A>    [3] => <A href="http://news.gmw.cn/newspaper/2014-02/08/content_2849438.htm" target="_blank">肇庆:主要景区迎客221万人次</A>    [4] => <A href="http://news.xinhuanet.com/edu/2014-02/08/c_126098505.htm" target="_blank">把对故乡眷恋藏心里 春节后娃娃随父母出发 组图 </A>    [5] => <A href="http://news.xinhuanet.com/edu/2014-02/08/c_119240720.htm" target="_blank">促进高校毕业生就业有哪些新的改革举措 </A>    [6] => <A href="http://learning.sohu.com/20140208/n394601900.shtml" target="_blank">借力“适应性学习” Knewton演绎在线教育变革</A>    [7] => <A href="http://www.sd.xinhuanet.com/news/2014-02/08/c_119235535.htm" target="_blank">山东为自费留学中介上紧箍 严禁到中小学招生 </A>    [8] => <A href="http://edu.people.com.cn/n/2014/0208/c1053-24295188.html" target="_blank">西汉“学术超男”策划“诗经秀”取功名</A>    [9] => <A href="http://edu.people.com.cn/n/2014/0208/c1053-24295191.html" target="_blank">李鸿章哥哥摆架子被下级痛骂</A>    [10] => <A href="http://www.jyb.cn/basc/xw/201402/t20140208_569160.html" target="_blank">江苏金坛教育系统工作“贴地行走”摸准基层脉</A>    [11] => <A class="more" href="http://news.baidu.com/n?cmd=1&class=edunews&pn=1" target="_blank">更多>></A>)Array(    [0] => <A href="http://news.ycwb.com/2014-02/08/content_6034510.htm" target="_blank">川大教授张兴栋当选美国国家工程院外籍院士 </A>)

感谢版主的回答,正则表达式也看过,没想到能这么用,完全解决了我的问题,知道我的需求,大师就是大师哦

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
Working with Flash Session Data in LaravelWorking with Flash Session Data in LaravelMar 12, 2025 pm 05:08 PM

Laravel simplifies handling temporary session data using its intuitive flash methods. This is perfect for displaying brief messages, alerts, or notifications within your application. Data persists only for the subsequent request by default: $request-

cURL in PHP: How to Use the PHP cURL Extension in REST APIscURL in PHP: How to Use the PHP cURL Extension in REST APIsMar 14, 2025 am 11:42 AM

The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

Simplified HTTP Response Mocking in Laravel TestsSimplified HTTP Response Mocking in Laravel TestsMar 12, 2025 pm 05:09 PM

Laravel provides concise HTTP response simulation syntax, simplifying HTTP interaction testing. This approach significantly reduces code redundancy while making your test simulation more intuitive. The basic implementation provides a variety of response type shortcuts: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

PHP Logging: Best Practices for PHP Log AnalysisPHP Logging: Best Practices for PHP Log AnalysisMar 10, 2025 pm 02:32 PM

PHP logging is essential for monitoring and debugging web applications, as well as capturing critical events, errors, and runtime behavior. It provides valuable insights into system performance, helps identify issues, and supports faster troubleshoot

12 Best PHP Chat Scripts on CodeCanyon12 Best PHP Chat Scripts on CodeCanyonMar 13, 2025 pm 12:08 PM

Do you want to provide real-time, instant solutions to your customers' most pressing problems? Live chat lets you have real-time conversations with customers and resolve their problems instantly. It allows you to provide faster service to your custom

Explain the concept of late static binding in PHP.Explain the concept of late static binding in PHP.Mar 21, 2025 pm 01:33 PM

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

Customizing/Extending Frameworks: How to add custom functionality.Customizing/Extending Frameworks: How to add custom functionality.Mar 28, 2025 pm 05:12 PM

The article discusses adding custom functionality to frameworks, focusing on understanding architecture, identifying extension points, and best practices for integration and debugging.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),