查找用户输入的内容与我的阵列中存储的内容有关的问题

时间:2017-03-08 18:32:10

标签: php html arrays regex validation

我遇到的问题是从已包含信息的数组中找到用户在网站前端所做的具体输入。

正如当前系统所示,如果一次输入一个项目,它将从数组中提取项目,但是当我组合输入时,它会检测输入两次,而不是将输入分成两个不同的数组。

我认为这是因为我使用的是strpos,而我无法找到可行的替代解决方案。因为我只想获取输入并将它们存储在正确的数组中,而不是在两个数组中都进行回声。

输入一个标签时的当前系统(打开和关闭):

打开代码:https://gyazo.com/6e459f6a1dfa517de7c2c18afc68e47d 结束标记:https://gyazo.com/48536708a89dd27601e5539ebde3fb51

输入两个标签时的当前系统:

输入两个标签:https://gyazo.com/4eb62fe51383f2cff70746e57c5d26c9

代码:

<?php
//Depricated
//$TagArray = $UserInput.split("");
if(isset($_POST['code'])){
    $UserInput = htmlspecialchars($_POST['code']);
    $InputtedTags = array();
    $InputtedOpenTags = array();
    $InputtedClosingTags = array();

    //Array Containing all of the VALID HTML TAGS.
    $AllowedTags = array("&lt;html&gt;","&lt;head&gt","&lt;body&gt","&lt;div&gt","&lt;p&gt","&lt;b&gt","&lt;base&gt","&lt;link&gt","&lt;meta&gt","&lt;style&gt","&lt;title&gt","&lt;address&gt","&lt;article&gt","&lt;aside&gt","&lt;footer&gt","&lt;h1&gt","&lt;h2&gt","&lt;h3&gt","&lt;h4&gt","&lt;h5&gt","&lt;h6&gt","&lt;header&gt","&lt;hgroup&gt","&lt;nav&gt","&lt;selection&gt","&lt;dd&gt","&lt;d1&gt","&lt;dt&gt","&lt;figcaption&gt","&lt;figure&gt","&lt;hr&gt","&lt;li&gt","&lt;main&gt","&lt;ol&gt","&lt;pre&gt","&lt;ul&gt","&lt;a&gt","&lt;abbr&gt","&lt;b&gt","&lt;bdi&gt","&lt;bdo&gt","&lt;br&gt","&lt;cite&gt","&lt;code&gt","&lt;data&gt","&lt;dfn&gt","&lt;em&gt","&lt;i&gt","&lt;kbd&gt","&lt;mark&gt","&lt;q&gt","&lt;rp&gt","&lt;rt&gt","&lt;rtc&gt","&lt;ruby&gt","&lt;s&gt","&lt;samp&gt","&lt;small&gt","&lt;span&gt","&lt;strong&gt","&lt;sub&gt","&lt;sup&gt","&lt;time&gt","&lt;u&gt","&lt;var&gt","&lt;wbr&gt","&lt;area&gt","&lt;audio&gt","&lt;img&gt","&lt;map&gt","&lt;track&gt","&lt;video&gt","&lt;embed&gt","&lt;object&gt","&lt;param&gt","&lt;source&gt","&lt;canvas&gt","&lt;noscript&gt","&lt;script&gt","&lt;del&gt","&lt;ins&gt","&lt;caption&gt","&lt;col&gt","&lt;colgroup&gt","&lt;table&gt","&lt;tbody&gt","&lt;td&gt","&lt;tfoot&gt","&lt;th&gt","&lt;thead&gt","&lt;tr&gt","&lt;button&gt","&lt;datalist&gt","&lt;fieldset&gt","&lt;form&gt","&lt;input&gt","&lt;label&gt","&lt;legend&gt","&lt;meter&gt","&lt;optgroup&gt","&lt;option&gt","&lt;output&gt","&lt;progress&gt","&lt;select&gt","&lt;textarea&gt","&lt;details&gt","&lt;dialog&gt","&lt;menu&gt","&lt;menuitem&gt","&lt;summary&gt","&lt;shadow&gt","&lt;slot&gt","&lt;template&gt","&lt;acronym&gt","&lt;applet&gt","&lt;basefont&gt","&lt;big&gt","&lt;blink&gt","&lt;center&gt","&lt;command&gt","&lt;content&gt","&lt;dir&gt","&lt;element&gt","&lt;font&gt","&lt;frame&gt","&lt;frameset&gt","&lt;isindex&gt","&lt;keygen&gt","&lt;listing&gt","&lt;marquee&gt","&lt;multicol&gt","&lt;nextid&gt","&lt;noembed&gt","&lt;plaintext&gt","&lt;shadow&gt","&lt;spacer&gt","&lt;strike&gt","&lt;tt&gt","&lt;xmp&gt","&lt;/html&gt","&lt;/head&gt","&lt;/body&gt","&lt;/div&gt","&lt;/p&gt","&lt;/b&gt","&lt;/base&gt","&lt;/link&gt","&lt;/meta&gt","&lt;/style&gt","&lt;/title&gt","&lt;/address&gt","&lt;/article&gt","&lt;/aside&gt","&lt;/footer&gt","&lt;/h1&gt","&lt;/h2&gt","&lt;/h3&gt","&lt;/h4&gt","&lt;/h5&gt","&lt;/h6&gt","&lt;/header&gt","&lt;/hgroup&gt","&lt;/nav&gt","&lt;/selection&gt","&lt;/dd&gt","&lt;/d1&gt","&lt;/dt&gt","&lt;/figcaption&gt","&lt;/figure&gt","&lt;/hr&gt","&lt;/li&gt","&lt;/main&gt","&lt;/ol&gt","&lt;/pre&gt","&lt;/ul&gt","&lt;/a&gt","&lt;/abbr&gt","&lt;/b&gt","&lt;/bdi&gt","&lt;/bdo&gt","&lt;/br&gt","&lt;/cite&gt","&lt;/code&gt","&lt;/data&gt","&lt;/dfn&gt","&lt;/em&gt","&lt;/i&gt","&lt;/kbd&gt","&lt;/mark&gt","&lt;/q&gt","&lt;/rp&gt","&lt;/rt&gt","&lt;/rtc&gt","&lt;/ruby&gt","&lt;/s&gt","&lt;/samp&gt","&lt;/small&gt","&lt;/span&gt","&lt;/strong&gt","&lt;/sub&gt","&lt;/sup&gt","&lt;/time&gt","&lt;/u&gt","&lt;/var&gt","&lt;/wbr&gt","&lt;/area&gt","&lt;/audio&gt","&lt;/img&gt","&lt;/map&gt","&lt;/track&gt","&lt;/video&gt","&lt;/embed&gt","&lt;/object&gt","&lt;/param&gt","&lt;/source&gt","&lt;/canvas&gt","&lt;/noscript&gt","&lt;/script&gt","&lt;/del&gt","&lt;/ins&gt","&lt;/caption&gt","&lt;/col&gt","&lt;/colgroup&gt","&lt;/table&gt","&lt;/tbody&gt","&lt;/td&gt","&lt;/tfoot&gt","&lt;/th&gt","&lt;/thead&gt","&lt;/tr&gt","&lt;/button&gt","&lt;/datalist&gt","&lt;/fieldset&gt","&lt;/form&gt","&lt;/input&gt","&lt;/label&gt","&lt;/legend&gt","&lt;/meter&gt","&lt;/optgroup&gt","&lt;/option&gt","&lt;/output&gt","&lt;/progress&gt","&lt;/select&gt","&lt;/textarea&gt","&lt;/details&gt","&lt;/dialog&gt","&lt;/menu&gt","&lt;/menuitem&gt","&lt;/summary&gt","&lt;/shadow&gt","&lt;/slot&gt","&lt;/template&gt","&lt;/acronym&gt","&lt;/applet&gt","&lt;/basefont&gt","&lt;/big&gt","&lt;/blink&gt","&lt;/center&gt","&lt;/command&gt","&lt;/content&gt","&lt;/dir&gt","&lt;/element&gt","&lt;/font&gt","&lt;/frame&gt","&lt;/frameset&gt","&lt;/isindex&gt","&lt;/keygen&gt","&lt;/listing&gt","&lt;/marquee&gt","&lt;/multicol&gt","&lt;/nextid&gt","&lt;/noembed&gt","&lt;/plaintext&gt","&lt;/shadow&gt","&lt;/spacer&gt","&lt;/strike&gt","&lt;/tt&gt","&lt;/xmp&gt");
    //$Tags = implode(",",$AllowedTags);
    //$OpenTags = implode(",",$AllowedTags);

    //Search Allowed Tags Array For Values Containing a Backslash(/)
    $AllowedClosingTags = array_filter($AllowedTags, function($val) {
        return (bool)preg_match('/\//', $val);
    });

    //print_r($AllowedClosingTags);

    //Search Allowed Tags Array For Values Not Containing a Backslack(/)   
    $AllowedOpeningTags = array_filter($AllowedTags, function($val) {
        return (bool)!preg_match('/\//', $val);
    });

    // print_r($AllowedOpeningTags);

    //Check What The User Has Inputted Into The System against the AllowedOpeningTags Array 
    //If it is true then display to the user the tag is valid
    //Push The value that the user entered onto the InputtedOpenTags Array
    foreach($AllowedTags as $data){
        if(strpos($UserInput,$data) !==false){
            echo($UserInput. ": Valid Tags Inputted </br>");
            array_push($InputtedTags,$UserInput);
        }
    }
    //print_r($InputtedOpenTags);  

    //Check What The User Has Inputted Into The System against the AllowedOpeningTags Array 
    //If it is true then display to the user the tag is valid
    //Push The value that the user entered onto the InputtedOpenTags Array
    foreach($AllowedOpeningTags as $data){
        if(strpos($UserInput,$data) !==false){
            echo($UserInput. ": Valid Opening Tags </br>");
            array_push($InputtedOpenTags,$UserInput);
        }
    }
    //print_r($InputtedOpenTags);

    //Check What The User Has Inputted Into The System against the AllowedClosingTags Array 
    //If it is true then display to the user the tag is valid
    //Push The value that the user entered onto the InputtedClosingTags Array   
    foreach($AllowedClosingTags as $data){
        if(strpos($UserInput,$data) !==false){
            echo($UserInput. ": Valid Closing Tags </br>");
            array_push($InputtedClosingTags,$UserInput);
        }
    }
    //print_r($InputtedClosingTags);

    $OTags = implode(",",$InputtedOpenTags);
    $CTags = implode(",",$InputtedClosingTags);
    $InputtedTags = array($OTags,$CTags);
    print_r($InputtedTags);
}

1 个答案:

答案 0 :(得分:0)

现在使用更小的验证数组,更少的循环/过滤,没有strpos,没有array_push。 (您的$AllowedTags个元素存在不一致性:一些元素带有&gt,另一些元素带有&gt;)这样更精简,更清洁:

查看PHP Demo

查看正则表达式Demo

以下是代码:

// don't duplicate tag names as /-prefixed and non-/-prefixed
$AllowedTags = array("html","head","body","div","p","b","base","link","meta","style",
"title","address","article","aside","footer","h1","h2","h3","h4","h5","h6","header",
"hgroup","nav","selection","dd","d1","dt","figcaption","figure","hr","li","main","ol",
"pre","ul","a","abbr","b","bdi","bdo","br","cite","code","data","dfn","em","i","kbd",
"mark","q","rp","rt","rtc","ruby","s","samp","small","span","strong","sub","sup","time",
"u","var","wbr","area","audio","img","map","track","video","embed","object","param",
"source","canvas","noscript","script","del","ins","caption","col","colgroup","table",
"tbody","td","tfoot","th","thead","tr","button","datalist","fieldset","form","input",
"label","legend","meter","optgroup","option","output","progress","select","textarea",
"details","dialog","menu","menuitem","summary","shadow","slot","template","acronym",
"applet","basefont","big","blink","center","command","content","dir","element","font",
"frame","frameset","isindex","keygen","listing","marquee","multicol","nextid","noembed",
"plaintext","shadow","spacer","strike","tt","xmp");

$UserInput="&lt;a href=&quot;https:www.this.that.com&quot;&gt;Word&lt;/a&gt;&lt;aaa style=&#39;color:orange;&#39;&gt;Somethingelse&lt;/aaa&gt;&lt;b&gt;Another bit of something&lt;/b you=can't put attributes on an end tag&gt;";
// <a href="https:www.this.that.com">Word</a><aaa style='color:orange;'>Somethingelse</aaa><b>Another bit of something</b you=can't put attributes on an end tag>

if(preg_match_all('/&lt;([^\/][a-z1-6]*)\s?(.*?)&gt;|&lt;\/([^&]*?)&gt;/',$UserInput,$tags)){
    // match opening, opening attributes, and closing tags in three separate groups,
    // but store only the tagname in tag groups and keep the attribute string whole
    $AllTags=array(
        "Opening"=>array_diff($tags[1],array('')),  // remove empties
        "Closing"=>array_diff($tags[3],array(''))  // remove empties
    );
    foreach($AllTags as $label=>$group){  // loop through opening & closing tags
        foreach(array_intersect($group,$AllowedTags) as $tag){ // loop Valids
            echo "<div style='color:green;'>Valid $label Tag: $tag</div>";
        }       
        foreach(array_diff($group,$AllowedTags) as $tag){  // loop Invalids
            echo "<div style='color:red;'>Invalid $label Tag: $tag </div>";
        }  
    }
    $Attributes=array_diff($tags[2],array(''));  // remove empties
    // As it is a separate can of worms, I will leave $Attributes handling to you.
    foreach($Attributes as $attr){ // loop Attributes
        echo "<div style='color:green;'>Attribute: $attr</div>";
    }     
}else{
    echo "<div style='color:blue;'>No tags found in input</div>";
}

这有助于您识别$UserInput中所有有效和无效的开始和结束标记。根据您的确切需求修改我的解决方案如有必要,我很乐意进一步解释。

*请注意,如果您的程序允许包含属性的标记,则需要扩展正则表达式模式以允许(而不是捕获)属性。