从文本中获取第一张图片的 smarty 插件
smarty plugin to get the first image from a text
我有一个聪明的变量输出这样的数组:
$article = Array(10)
id => "103"
categoryid => "6"
title => "¿Cuánto espacio necesito para mi siti..."
text => "<img class="img-responsive center img..."
我需要从 $article.text 中提取第一张图片 url 并将其显示在模板上。因为我想动态创建facebook og:image 属性 tag:
<meta property="og:image" content="image.jpg" />
我知道在 php 上此代码有效:
$texthtml = $article['text'];
preg_match('/<img.+src=[\'"](?P<src>.+)[\'"].*>/i', $texthtml, $image);
return $image['src'];
但我不想使用来自 smarty 的 {php} 标签,因为它们已被弃用。
所以我只是用下面的代码构建了一个 smarty 插件:
* Smarty plugin
* -------------------------------------------------------------
* File: function.articleimage.php
* Type: function
* Name: articleimage
* Purpose: get the first image from an array
* -------------------------------------------------------------
*/
function smarty_function_articleimage($params)
{
$texthtml = $article['text'];
preg_match('/<img.+src=[\'"](?P<src>.+)[\'"].*>/i', $texthtml, $image);
return $image['src'];
}
然后我像这样将它插入到模板中:
<meta property="og:image" content="{articleimage}" />
但它不起作用:(
有什么线索吗?
看来您需要将 $article
传递给函数。
在Smarty Template Function documentation中说:
All attributes passed to template functions from the template are contained in the $params as an associative array.
基于this documentation,传递变量的语法看起来像这样:
{articleimage article=$article}
然后在函数中,你应该可以像这样从$params
得到它:
function smarty_function_articleimage($params)
{
$text = $params['article']['text'];
preg_match('/<img.+src=[\'"](?P<src>.+)[\'"].*>/i', $text, $image);
return $image['src'];
}
我有一个聪明的变量输出这样的数组:
$article = Array(10)
id => "103"
categoryid => "6"
title => "¿Cuánto espacio necesito para mi siti..."
text => "<img class="img-responsive center img..."
我需要从 $article.text 中提取第一张图片 url 并将其显示在模板上。因为我想动态创建facebook og:image 属性 tag:
<meta property="og:image" content="image.jpg" />
我知道在 php 上此代码有效:
$texthtml = $article['text'];
preg_match('/<img.+src=[\'"](?P<src>.+)[\'"].*>/i', $texthtml, $image);
return $image['src'];
但我不想使用来自 smarty 的 {php} 标签,因为它们已被弃用。
所以我只是用下面的代码构建了一个 smarty 插件:
* Smarty plugin
* -------------------------------------------------------------
* File: function.articleimage.php
* Type: function
* Name: articleimage
* Purpose: get the first image from an array
* -------------------------------------------------------------
*/
function smarty_function_articleimage($params)
{
$texthtml = $article['text'];
preg_match('/<img.+src=[\'"](?P<src>.+)[\'"].*>/i', $texthtml, $image);
return $image['src'];
}
然后我像这样将它插入到模板中:
<meta property="og:image" content="{articleimage}" />
但它不起作用:(
有什么线索吗?
看来您需要将 $article
传递给函数。
在Smarty Template Function documentation中说:
All attributes passed to template functions from the template are contained in the $params as an associative array.
基于this documentation,传递变量的语法看起来像这样:
{articleimage article=$article}
然后在函数中,你应该可以像这样从$params
得到它:
function smarty_function_articleimage($params)
{
$text = $params['article']['text'];
preg_match('/<img.+src=[\'"](?P<src>.+)[\'"].*>/i', $text, $image);
return $image['src'];
}