【问题标题】:Extracting an image URL from arbitrary text从任意文本中提取图像 URL
【发布时间】:2017-03-08 23:02:39
【问题描述】:

嘿,我需要从这段文字中提取第一张图片:

[size=5]test.[/size]
 
[size=5]first.[/size]
 
[img=http://i64.tinypic.com/n337ap.png]
 
[size=5]Added:[/size]
 
[img=http://i63.tinypic.com/2m578i1.png]
 
[img=http://i65.tinypic.com/ev7653.png]

我要提取的示例:http://i64.tinypic.com/n337ap.png

【问题讨论】:

  • 我看不到你目前的代码......

标签: php html regex


【解决方案1】:

正确的正则表达式是:

<?php

$text = ' [size=5]test.[/size]   [size=5]first.[/size]   [img=http://i64.tinypic.com/n337ap.png]   [size=5]Added:[/size]   [img=http://i63.tinypic.com/2m578i1.png]   [img=http://i65.tinypic.com/ev7653.png]';

preg_match("/(?<=\[img\=)([a-z:\/0-9.]+)(?=\])/i",$text,$out, PREG_OFFSET_CAPTURE);

print($out[0][0]);

?>

【讨论】:

  • 谢谢!你这么多:D!
猜你喜欢
  • 2011-01-09
  • 2014-07-31
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多