dongpangbu4016 2017-05-31 19:07
浏览 101
已采纳

在括号内提取文本的最佳方法 - PHP解析

What's the best way to get text from a string. I have a LONGTEXT field in the db and currently is:

<ul>
 <li>Winner on Laps #153 LemonAid Racing (1996 Geo Metro, Davenport Iowa)</li>
 <li>Winner on Index of Effluency #42 J CrewD (1985 Jagaur XJ6, Knoxville Tn)     </li>
 <li>Winner, Class A (The Good) #153 LemonAid Racing (1996 Geo Metro, Davenport Iowa)</li>
 <li>Winner, Class B (The Bad) #128 Duff Beer (1994 Honda Civic, Raleigh NC and Atlanta GA)</li>
 <li>Winner, Class C (The Ugly) #64 Team Fairlylame (1964 Ford Fairlane, Savannah GA)</li>
 <li>Organizer's Choice #54 Knoxvegas Lowballers (1998 Ford Contour SVT, Knoxville TN)</li>
 <li>Most Heroic Fix #40 Mock Grass Racing (1998 Kia Sephia, Columbia SC)</li>
 <li>Judges' Choice #85 Apocalyptic Racing (1978 Toylet Celica, St Louis mo)     </li>
 <li>Judges' Choice #2 #17 Fireball Racing (1991 Ford Escort, Aurora IL)</li>
</ul>

1 Say I wanted to get the strings "1996 Geo Metro, Davenport Iowa", "1985 Jagaur XJ6, Knoxville Tn".. etc... from the entire string (the text inside the parenthesis).What is the best way to do so?

  1. Say I wanted to get all the text in the 'li' up to the symbol "#", is that possible?

Thanks in advance

  • 写回答

2条回答 默认 最新

  • douzhu3654 2017-05-31 19:24
    关注

    You can try using Regular Expressions.

    For example, to find (the text inside the parenthesis) you can use the following regex:

    /(?<=\()([^\)]+)(?=\))/
    

    and to get all the text in the 'li' up to the symbol "#" you can use:

    /(?<=<li>).*(?=#)/
    

    Perform a regular expression match on PHP

    Awesome regex resource!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?