Smarty:使用正则表达式并获取输出 (Prestashop)

Smarty : use regex and get output (Prestashop)

我尝试使用 Smarty 从 HTML 变量中仅获取 first src img 路径(我在 Prestashop 中使用它)。

我有这个变量:

{$cmspages.content}

并且需要从中提取第一个图像源,例如:

I am a butterfly <img src="http://www.localhost.com" /> you know ?

我需要:

http://www.localhost.com

我确实尝试了一些 regex 命令或 strstr 但没有任何结果。

在 Smarty 中,您可以像这样在模板语法之外使用变量:

$this->get_template_vars('cmspages').content

获取第一个 img 的 src:

preg_match("~<img.+?src=\"([^"]+)\"~", $this->get_template_vars('cmspages').content, $match);

scr 属性的值存储在 $match[1]