douzuo0711 2011-10-10 22:43
浏览 11
已采纳

PREG_MATCH_ALL参加最后一场比赛

hello im just having a problem here with PREG_MATCH_ALL() lets say i have 2 strings ... Save You and What If Uploader Name i need to retreive Save You so i'm using the folowing paterns

preg_match_all('/<span id="title.*">(.*)<\/span>/i',,);

as a result i get

Save You Uploader Name

I think this is because it's taking the last any way to fix it ? or like matching the first ? thank you

EDIT:

<a href="#" onclick="searchActions.showLyrics('14840_100733666_21',9965243); return false;">Can&#39;t Keep My Hands Off You</a></span>

get as result

<a href="Your Love Is A Lie"</a>

with

/<span id="title(.*?)">(.*?)<\/span>/i
  • 写回答

1条回答 默认 最新

  • duangou1551 2011-10-10 22:54
    关注

    The reason is that .* is greedy. The first .* (right after title) will swallow as many characters as it can, and then backtrack from the end of the string until the rest of the regexp can match.

    To fix it, either use the ungreedy version .*?, and/or replace the . with some character class that can't match a quote (such as [^"]):

    preg_match_all('/<span id="title[^"]*">(.*?)<\/span>/i', ..., ...);
    

    For more information, see http://www.php.net/manual/en/regexp.reference.repetition.php

    (Ps. Levi Morrison is right — this is not a good way to parse HTML. Use a real HTML parser, such as DOMDocument::loadHTML().)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建