Php 如果条件作用于变量的内容(如果包含字符)

Php 如果条件作用于变量的内容(如果包含字符),php,smarty3,Php,Smarty3,要为Facebook创建合适的og:description,我希望文本文章的前200个字符必须用作描述(否则它将只是标题) 因此,我在标题中写道 {if not empty($post_info.parsed_excerpt)} <meta name="description" content="{$post_info.parsed_excerpt|truncate:200|escape}" property="og:description"> {else} <meta nam

要为Facebook创建合适的og:description,我希望文本文章的前200个字符必须用作描述(否则它将只是标题)

因此,我在标题中写道

{if not empty($post_info.parsed_excerpt)}
<meta name="description" content="{$post_info.parsed_excerpt|truncate:200|escape}" property="og:description">
{else}
<meta name="description" content="{$post_info.title|escape} - {$blog_data.title|escape}" property="og:description">
{/if}
{如果不是空的($post\u info.parsed\u摘录)}
{else}
{/if}
我以为我做对了,但我发现了例外。 文章可能包含比可读文本更多的内容,图像插件也可以在那里
。我甚至有一个例子,没有文本内容,只有一个图像

所以我需要适应它。 我找到了strip_标签修改器,所以我做了一些调整
{$post_info.parsed_摘录| truncate:200 | escape}
{$post_info.parsed_摘录| truncate:200 | strip|u标签| escape}
描述现在没有任何标签。完成一半!:)

我仍然需要第一个条件
{if not empty($post_info.parsed_extract)}
来查找没有标记的
$post_info.parsed_extract
,我不知道如何查找


谢谢你的帮助

我一点也不精通smarty3,事实上这是我第一次听说它。另外,我也不完全确定您使用新的if语句想要实现什么,您是否要检查字符串(不包括HTML标记及其属性)是否为空?如果是这样,您不能在条件之前去除标记,请将其分配给一个新变量,然后检查它,例如(请原谅这是一种伪代码,而不是smarty,如前所述,我不熟悉它)
$execrpt\u不带标记=去除标记($post\u info.parsed\u execrpt);
if(不为空($EXTRACTION_,无_标记)){
//做事
}

另外,仅从标题中提到的if contains char开始,看看php strpos()函数,同样不确定该函数是否支持smarty

希望这能对你有所帮助,如果我误解了,请原谅


编辑:格式化

您最好先去掉标签,然后截断为200:
{$post_info.parsed_摘录| strip_标签| truncate:200 | escape}
第一个条件是什么?我不明白。。。对另一个变量执行相同的操作。。。