如何从数据库列中提取img src? [英] How to extract img src from database column?

查看:67
本文介绍了如何从数据库列中提取img src?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据库列,用于存储多个图像的文本和img src。这是一个有文字和多个img src的新闻帖子。图像或文本没有预定义的序列,所有这些都留在用户发布帖子上。现在我想在该列中提取第一个img src并将其用作字符串。刚学到像html解析这样的东西是神奇的,但我不是编程专家,所以我怎么可能这样做呢?我通过调用该行显示整个帖子(文本,图像)<?php echo $ row ['body']?>所以这是带有img src图像的行。



我尝试过的事情:



试过这个但不起作用。

$ html =< img src =。$ row ['body']。alt ='Image'/> ;

$ src =(string)reset(simplexml_import_dom(DOMDocument :: loadHTML($ html)) - > xpath(// img / @src));

I have a database column which stores text and img src for multiple images. It's kind of body of news post having text and multiple img src. There is no predefined sequence for images or text, it's all left on user posting the post. Now I want to extract the first img src in that column and use it as a string. Just learned something like html parsing is to the magic but I am not expert in programming so how could I possibly do that? I show the whole post (text,images) by calling that row <?php echo $row['body'] ?> so this the the row that carries img src of images.

What I have tried:

Tried this but does not works.
$html = "<img src=".$row['body']." alt='Image' />";
$src = (string) reset(simplexml_import_dom(DOMDocument::loadHTML($html))->xpath("//img/@src"));

推荐答案

row ['body']?>所以这是带有img src图像的行。



我尝试过的事情:



尝试了这个但是没有用。
row['body'] ?> so this the the row that carries img src of images.

What I have tried:

Tried this but does not works.


html =< img src =。
html = "<img src=".


row ['body']。alt ='图像'/>;
row['body']." alt='Image' />";


这篇关于如何从数据库列中提取img src?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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