我正在用 PHP5 和 MySql 编写元素代码。它包括标题、内容。在内容中,它包含图像链接。我想在内容中获取图像链接,所以我使用 Preg_replace 来实现。但效果不佳,有时,图片链接包含另一个标签,Preg_replace 不适用于此图片链接。
这是我的代码
preg_match('/<img.+src=[\'"](?P<src>.+)[\'"].*>/i', $Content, $image);
$image = preg_replace( '/(width|height)="\d*"\s/', "", $image );
$image = preg_replace( '/"/', "", $image );
$image = preg_replace( '/height=\d*/', "", $image );
$image = preg_replace( '/(width|height)=\d*/', "", $image );
我尽量剪掉图片链接中的标签。但有时,我从另一个网站复制的内容包括图像链接为:
<img alt="" height="20" src="http://anotherWebsite.com/images/2014/share-gg.gif" style="border: 0px; max-width: 400px;" width="67" />
而且它不是固定图像链接。
所以,请帮助我使用 Preg_replace,它可以很好地处理所有图像链接。谢谢!
P/s: CkEditor 创建的内容
最佳答案
你在寻找这样的东西吗?
preg_match('/<img.+src=[\'"](?P<src>.+)[\'"].*>/i', $Content, $image);
preg_match('/src="(.*?)"/',$image[0],$image);
$image = (isset($image[1]))?$image[1]:'';
它使用 src 搜索 img 标签,然后在找到的标签内搜索 src 的内部内容
关于javascript - Preg_replace 在 Content PHP 中获取图片链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33390251/