Smarty匹配文本,如SQL LIKE语句

时间:2015-08-21 07:31:37

标签: smarty

我想知道有没有办法聪明地做一些像MySQL LIKE这样的事情。 我有:

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

如果$value中有text1,则想要做点什么。

2 个答案:

答案 0 :(得分:1)

感谢help from Raulucco,我写了以下代码:

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

这对我有用。

答案 1 :(得分:0)

您的解决方案更简单,更好。我走得更复杂了:-) 你的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"))}
相关问题