(PHP)拒绝不包含必需标记的描述

时间:2011-06-06 11:13:46

标签: php

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

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>

感谢您的帮助!

3 个答案:

答案 0 :(得分:2)

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

$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

缺少参数

答案 1 :(得分:0)

1将标签插入数组

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

foreach()

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

3显示错误

答案 2 :(得分:0)

$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解决方案的替代方案。