(PHP)拒绝没有;t包括所需的标签


(PHP) Reject descriptions that don't include the required tags

当描述太短时,这就是我的脚本当前发送错误的方式:

if(strlen($linkres->content) = minStoryLength ) { // if  description is too short
        $main_smarty->assign('submit_error', 'incomplete');
        $main_smarty->display($the_template . '/submit_errors.tpl');
        $error = true;
    }

当描述不包括以下标签/单词中的至少一个时,我想发送相同的错误消息:

<img><youtube><googlevideo><xoinks><break><vimeo><revver><myspace>
<veoh><wmv><dailymotion><ifilm><metacafe><tubeley><guba>

谢谢你的帮助!

我会在这里使用一些正则表达式来进行负匹配

$tags = array("img", "youtube", "googlevideo", "xoinks", "break", "vimeo", "revver", "myspace", "veoh", "wmv", "dailymotion", "ifilm", "metacafe", "tubeley", "guba");
if ((strlen($linkres->content) < minStoryLength) // if  description is too short
 || (!preg_match("'[<](".implode("|",$tags).")[^>]*[>]'is",$linkres->content)))
// or it does not contain any of the above
{ 
    $main_smarty->assign('submit_error', 'incomplete');
    $main_smarty->display($the_template . '/submit_errors.tpl');
    $error = true;
}

编辑:对进行了一点更正

EDIT2:我缺少preg_match 的参数

$tags = array('img','youtube','googlevideo','xoinks','break','vimeo','revver','myspace','veoh','wmv','dailymotion','ifilm','metacafe','tubeley','guba');
$contains = false;
foreach($tags as $check)
{
    if(preg_match("/<".$check.">/", $linkres->content))
    { 
        $contains = true;
    }
}
if(!$contains) 
{ 
   // here goes your error
}

这是inti解决方案的替代方案。

1在数组中插入标签

2迭代数组并检查值是否与匹配

foreach()

http://www.php.net/manual/en/control-structures.foreach.php

3显示错误