Php Yii2编解码器小部件

Php Yii2编解码器小部件,php,image,yii2,thumbnails,redactor,Php,Image,Yii2,Thumbnails,Redactor,我有一个基本的博客应用程序,它使用redactor作为输入文本的手段。但是,当上传图像时,它们会作为HTML标记保存在“body”字段中。例如: 测试博客帖子 是保存在“body”字段中的内容 以下是我检索和显示数据的方式: foreach($dataProvider->getModels() as $post){ echo Html::a('<div class="well well-sm" id="inner"><h3>'.$post->subj

我有一个基本的博客应用程序,它使用redactor作为输入文本的手段。但是,当上传图像时,它们会作为HTML标记保存在“body”字段中。例如:

测试博客帖子

是保存在“body”字段中的内容

以下是我检索和显示数据的方式:

 foreach($dataProvider->getModels() as $post){
    echo Html::a('<div class="well well-sm" id="inner"><h3>'.$post->subject.'</h3>', '/post/view?id=' .$post->id);
    echo 'Posted By: ' .$post->user->username . ' <br/><i class = "glyphicon glyphicon-time"></i> '.Yii::$app->formatter->asDatetime($post->updated_at)  . '<br/>Views: ' .$post->view_count .'</div>'; 
}   
foreach($dataProvider->getModels()作为$post){
echo Html::a('.$post->subject'.','/post/view?id='.$post->id);
echo“发布人:”。$post->user->username。“
”。Yii::$app->formatter->asDatetime($post->updated_at)。“
视图:”。$post->view_count。“; }
有没有一种方法可以添加一个“如果”语句来表示

if($post->body包含标记)
{display}

strstr()
搜索字符串中的字符串

所以你可以试试:

if (strstr($post->body, '<img ')) {
if(strstrstr($post->body,”
'; $regex='#“

这是密码


这是正则表达式

非常感谢我所需要的。你知道我如何从身体中提取图像标记吗?这可能不是最好的方法。你可以将其加载到DOMDocument中,然后按标记获取元素。另一种方法是尝试正则表达式匹配(不推荐)
<?php

$html = '<html>
<head>
<etc />
</head>
<body>
<div><img src="blah.jpg" /></div><br />
</body>
</html>';

$regex = '#<img .*?\/>#';

preg_match($regex, $html, $matches);

var_dump($matches[0]);