【问题标题】:PHP Get image url from user message (string) doesn't work with preg_match_allPHP 从用户消息(字符串)获取图像 url 不适用于 preg_match_all
【发布时间】:2015-01-27 22:45:23
【问题描述】:

你能告诉我我做错了什么吗? 它总是告诉我它是图像,即使它不是。

编辑:如果有图片链接,我需要获取图片链接。

while ( $row = mysql_fetch_assoc( $result ) ){
$msg = "<div class='msg'>[" . $row["time"] . "] <b>" . $row["name"] . ":</b> " . $row["message"];
echo $msg;

    preg_match_all( "/(https?:\/\/\S+\.(?:jpg|png|gif))\s+/", $msg, $urls );
if ( $urls ){
    echo "ye, its image";
    }
}

【问题讨论】:

  • 如果您在调用preg_match_all() 之后执行print_r($urls);,它会说什么?
  • $msg 中的内容然后:Array( [0] => Array ( ) [1] => Array ( ) )

标签: php html string image url


【解决方案1】:

没有我想的那么难。

while ( $row = mysql_fetch_assoc( $result ) ){
$msg = "<div class='msg'>[" . $row["time"] . "] <b>" . $row["name"] . ":</b> " . $row["message"];
echo $msg;

    preg_match_all( "/(https?:\/\/\S+\.(?:jpg|png|gif))\s+/", $msg, $urls );
if ( $urls ){
    echo '<img src=".$urls.">';
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-05-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多