Smarty 匹配文本,例如 SQL LIKE 语句

Smarty match text like SQL LIKE statement

我想知道在 smarty 中有没有办法做类似 MySQL LIKE 的事情。 我有:

$value = 'text1/text2';//possible values (text1/text2,text1,text2)

如果 $value 中有一个 text1.

,我想做点什么

感谢,我编写了以下代码:

{assign var=done value="/"|explode:$value}
{if in_array($serch, $done)}

这对我有用。

你的解决方案更简单更好。我要走更复杂的路:-) 在您的 php 文件中的某些位置:

<?php
$smarty->register_function('get_texts_match', 'text_exists_on_db');

function text_exists_on_db($text) {
  $conn = new PDO($dsn,$user,$pass);
  $q = $conn->prepare("Select text from texts where text like concat('%', ?, '%')");
  $q->execute(array($text));
  return $q->fetchAll(\PDO::FETCH_NUM);
}

和模板:

{if (in_array('text1', text_exists text="text1"))}