正则表达式 - 如何在第一次出现角色时停止 [英] regex- how to stop at first occurrence of a character

查看:147
本文介绍了正则表达式 - 如何在第一次出现角色时停止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从标签中提取src值,
到目前为止我似乎能够提取src值和字符串中最终引号之间的字符串

I am trying to extract the src value from a tag, so far I seem to be able to extract the string between the src value and the final quotation mark in the string

字符串:

<img  border="0"  src="http://i.bookfinder.com/about/booksellers/logo_borderless/amazon_uk.gif" width="89" height="31" alt="">

例如。在PHP中:

preg_match('/src=\"(.*)\"/', $row->find('a img',0), $matches);
if($matches){
   echo $matches[0];
}

打印出
src =http://i.bookfinder.com/about/booksellers/logo_borderless/amazon_uk.gifwidth =89height =31alt =

但我真正想要打印的是......
src =http://i.bookfinder。 com / about / booksellers / logo_borderless / amazon_uk.gif

或者如果可能的话......
http://i.bookfinder.com/about/booksellers/logo_borderless/amazon_uk.gif

什么我应该加入正则表达式吗?谢谢

what should I be adding to the regex? Thanks

推荐答案

对于RegExp:

preg_match('/src="([^"]+)"/', $row->find('a img',0), $matches);
echo $matches[1];

如果我是对的,您正在使用 simple_html_dom_parser 库。如果这是真的你只需输入:

If i'm right, you are working with simple_html_dom_parser library. If that's true you can just type:

$row->find('a img',0)->src

这篇关于正则表达式 - 如何在第一次出现角色时停止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆