search
HomeBackend DevelopmentPHP TutorialPHP正则表达式的匹配

今天主要的任务就是对新闻、研究报告等咨询的最近上传时间和本地的时间进行对比,比较一下是否超过1个小时,然后我对此进行了php的编程

刚开始,我本来以为主页中就有所需要处理的文字的信息,但是我找了好久就是没有发现所要查找的文字的信息,刚开始的时候我并没有发现然后一直在那傻傻地用正则表达式处理html的源代码,后来我发现原来是我自己的问题,原来html源代码中并不包含新闻的代码。然后我就用chrome对页面进行查找,终于找到了新闻页面来源的url。然后对url进行处理。

在对url进行处理的时候,我就直接用正则表达式处理,可是在处理的时候出现了各种各样的问题。首先时间字符串的长度随着时间的变化可能会变得不一样,所以一开始我并不知道怎么去处理,然后尝试了好久,后来,我尝试用.*的方式对文字进行匹配,可是因为在php中默认采用的是贪婪匹配,所以一匹配就是匹配所有的东西,然后我就对此进行搜索,查找解决的方法,找了很久都不能找到。

后来,我问了一下我的一个同学,他和我说了正则表达式默认是采用贪婪匹配的方式,而可以改变模式为非贪婪匹配就可以解决这个问题。

打个比方说,有一段html:<div>asdef<em>ccda</em>aae<br>sdc</div><div>asdef<em>dfge</em><br>sdc</div>。我想通过preg_match_all利用规则<div>\s*<br>将<div>asdef<em>ccda</em>aae<br>匹配出来,但是PHP好像并没有非贪婪模式的选项,导致匹配出来的是内容是<div>asdef<em>ccda</em>aae<br>sdc</div>
<div>asdef<em>dfge</em>,因为贪婪模式匹配到了后面的<br>了  <pre name="code" class="sycode">/正则/U 参数U,用书上的原话是,不再贪婪

然后我终于解决了问题

<?php /*判断最近更新时间是否大于一个小时函数*/function limittimeindex($url){	$info=file_get_contents($url);/*获取url的页面*/	preg_match('/date.*\",/U',substr($info,0),$m,PREG_OFFSET_CAPTURE);/*正则匹配字符串*/	$time=substr($m[0][0],7,-2);/*获取最新的网页更新时间*/	$systime=date("Y-m-d H:i:s");/*获取系统时间*/	if(strtotime($systime)-strtotime($time)>=3600){/*比较系统时间是否大于最近更新时间1小时,如果是则错误,如果不是则正确*/		echo "this is false<br>";	}	else{		echo "this is true<br>";	}	echo $time,"<br>";	echo $systime;}echo "
";if(isset($_POST['url']) && $_POST['url']!=''){ $url2=$_POST['url']; limittimeindex($url2);}?>

http://zhidao.baidu.com/question/110658951.html?qq-pf-to=pcqq.c2c

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
PHP Performance Tuning for High Traffic WebsitesPHP Performance Tuning for High Traffic WebsitesMay 14, 2025 am 12:13 AM

ThesecrettokeepingaPHP-poweredwebsiterunningsmoothlyunderheavyloadinvolvesseveralkeystrategies:1)ImplementopcodecachingwithOPcachetoreducescriptexecutiontime,2)UsedatabasequerycachingwithRedistolessendatabaseload,3)LeverageCDNslikeCloudflareforservin

Dependency Injection in PHP: Code Examples for BeginnersDependency Injection in PHP: Code Examples for BeginnersMay 14, 2025 am 12:08 AM

You should care about DependencyInjection(DI) because it makes your code clearer and easier to maintain. 1) DI makes it more modular by decoupling classes, 2) improves the convenience of testing and code flexibility, 3) Use DI containers to manage complex dependencies, but pay attention to performance impact and circular dependencies, 4) The best practice is to rely on abstract interfaces to achieve loose coupling.

PHP Performance: is it possible to optimize the application?PHP Performance: is it possible to optimize the application?May 14, 2025 am 12:04 AM

Yes,optimizingaPHPapplicationispossibleandessential.1)ImplementcachingusingAPCutoreducedatabaseload.2)Optimizedatabaseswithindexing,efficientqueries,andconnectionpooling.3)Enhancecodewithbuilt-infunctions,avoidingglobalvariables,andusingopcodecaching

PHP Performance Optimization: The Ultimate GuidePHP Performance Optimization: The Ultimate GuideMay 14, 2025 am 12:02 AM

ThekeystrategiestosignificantlyboostPHPapplicationperformanceare:1)UseopcodecachinglikeOPcachetoreduceexecutiontime,2)Optimizedatabaseinteractionswithpreparedstatementsandproperindexing,3)ConfigurewebserverslikeNginxwithPHP-FPMforbetterperformance,4)

PHP Dependency Injection Container: A Quick StartPHP Dependency Injection Container: A Quick StartMay 13, 2025 am 12:11 AM

APHPDependencyInjectionContainerisatoolthatmanagesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itactsasacentralhubforcreatingandinjectingdependencies,thusreducingtightcouplingandeasingunittesting.

Dependency Injection vs. Service Locator in PHPDependency Injection vs. Service Locator in PHPMay 13, 2025 am 12:10 AM

Select DependencyInjection (DI) for large applications, ServiceLocator is suitable for small projects or prototypes. 1) DI improves the testability and modularity of the code through constructor injection. 2) ServiceLocator obtains services through center registration, which is convenient but may lead to an increase in code coupling.

PHP performance optimization strategies.PHP performance optimization strategies.May 13, 2025 am 12:06 AM

PHPapplicationscanbeoptimizedforspeedandefficiencyby:1)enablingopcacheinphp.ini,2)usingpreparedstatementswithPDOfordatabasequeries,3)replacingloopswitharray_filterandarray_mapfordataprocessing,4)configuringNginxasareverseproxy,5)implementingcachingwi

PHP Email Validation: Ensuring Emails Are Sent CorrectlyPHP Email Validation: Ensuring Emails Are Sent CorrectlyMay 13, 2025 am 12:06 AM

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

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.

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

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),