用正则表达式求图片字符串

Fengxiubing2000 2005-10-22 04:13:59
$contant="aaa<img src='../images/bannersms.jpg'>aaa";
preg_match_all("/<img(?:\s+\w+=[^\s>]+)*\s+src=([^\s>]*)/gim",$contant,$a);
echo(count($a));

这样运行后出错...
请问哪位高手知道应该怎样改
...全文
412 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
Fengxiubing2000 2005-10-27
  • 打赏
  • 举报
回复
哈哈,,谢谢楼上各位高手!果然是stripslashes这个问题!现在用平生一笑的方法搞掂了!谢谢!还有稻草人,谢谢了!
ice_berg16 2005-10-26
  • 打赏
  • 举报
回复
我不知道你是如何写的代码,
下面的代码在我这里测试通过
<?php
$contant = <<<H
<table>
<tbody>
<tr>
<td><img alt="/" src="../img/a.jpg" /> </td>
</tr>
</tbody>
</table>
H;
preg_match_all("/<img.+src=('|\")(.+)\\1/isU",$contant,$a);
print_r($a);
?>
输出

Array
(
[0] => Array
(
[0] => <img alt="/" src="../img/a.jpg"
)

[1] => Array
(
[0] => "
)

[2] => Array
(
[0] => ../img/a.jpg
)

)
liyanyouth 2005-10-26
  • 打赏
  • 举报
回复
这是金山刚不久招聘的PHP的笔试~,完全不会做.
thunderx 2005-10-26
  • 打赏
  • 举报
回复
可以先做stripslashes,再析取;
很简单,如:
$str = stripslashes($str);
preg_match_all("/<img.+src=\"(.+)\"/Ui",$str,$out);
print_r($out[1]);
Gdj 2005-10-26
  • 打赏
  • 举报
回复
那应该是你的php.ini里设置了magic_quotes_gpc=On
- -b
Fengxiubing2000 2005-10-26
  • 打赏
  • 举报
回复
我发觉$contant 的内容在窗体提交后变了样:
<table>
<tbody>
<tr>
<td><img alt=\"/\" src=\"../img/a.jpg\" /> </td>
</tr>
</tbody>
</table>
Fengxiubing2000 2005-10-25
  • 打赏
  • 举报
回复
谢谢楼上,,,因为$contant的内容是一个文本框提交的,里面会有回车,例如:

<table cellspacing=0 cellpadding=0>
<tbody>
<tr>
<td>
<div align="center"><img alt='' src='../imgs/news/051019/0936372.jpg' /> </div>
<a href='../imgs/news/051019/0936371.jpg'>fd</a> </td>
</tr>
</tbody>
</table>

这样,就求不出来了,应该怎样解决呢?
Fengxiubing2000 2005-10-25
  • 打赏
  • 举报
回复
不行!
返回的没有src的值。
文本框的内容是:
<table>
<tbody>
<tr>
<td><img alt="/" src="../img/a.jpg" /> </td>
</tr>
</tbody>
</table>
preg_match_all("/<img.+src=('|\")(.+)\\1/isU",$contant,$a);
print_r($a);

返回的值:Array ( [0] => Array ( ) [1] => Array ( ) [2] => Array ( ) )
Gdj 2005-10-25
  • 打赏
  • 举报
回复
加了回车也求得的。草人的参数用了isU,i=不分大小写,s=通配符包括回车,U=默认不重复
Gdj 2005-10-24
  • 打赏
  • 举报
回复
草人的正解
gu1dai 2005-10-24
  • 打赏
  • 举报
回复
你是想匹配image的地址吧。
$contant="aaa<img src='../images/bannersms.jpg'>aaa";
preg_match_all("/<img[^s]+src="([^"]+)">/i",$contant,$a);
print_R($a);

ice_berg16 2005-10-22
  • 打赏
  • 举报
回复
$contant="aaa<img src='../images/bannersms.jpg'>aaa";
preg_match_all("/<img.+src=('|\")(.+)\\1/isU",$contant,$a);
print_r( $a );

21,887

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧