【问题标题】:Issue with finding what a user has inputted with what is aleady stored in my array查找用户输入的内容以及存储在我的数组中的内容的问题
【发布时间】:2017-03-08 18:32:09
【问题描述】:

我在查找用户从已包含信息的数组中输入网站前端的特定输入时遇到问题。

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

我认为这是因为我使用的是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);
}

【问题讨论】:

  • 那么您希望我为每个测试在系统中输入另一个标签,就像输入的打开、关闭和打开和关闭标签一样。
  • 不,抱歉,我会删除该评论。我设法构建了我自己的 $UserInput 字符串,该字符串应该与您的代码提供的内容相匹配。如果我的字符串不能准确描述您希望处理的字符串质量,请仅提供新的示例字符串。例如。如果您的用户的字符串可以包含带有属性的标签,请提供一个真实的示例,以便我可以更新正则表达式模式
  • 目前我只是检查主标签是打开还是关闭,因为我没有所有现有属性的数组。但是,由于用户输入来自用户输入数据的表单,因此我必须从表单中获取输入。
  • 如果我能够检查标签的属性,例如图像或表单元素,那就太好了,但我目前没有数据集来在我预先存在的系统中进行这项工作。跨度>

标签: php html arrays regex validation


【解决方案1】:

现在使用更小的验证数组,更少的循环/过滤,没有 strpos,没有 array_push。 (您的 $AllowedTags 元素有不一致之处:一些与 &amp;gt 和一些与 &amp;gt;)这是更精简和更清洁:

查看PHPDemo

查看正则表达式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 中所有有效和无效的开始和结束标记。根据您的确切需求修改我的解决方案。如有必要,我很乐意进一步解释。

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

【讨论】:

  • 对不起,我昨晚被送进医院时无法看到这个答案并检查解决方案。我明天一回家就会测试这个解决方案,并会更新你的状态。不过还是谢谢你的回答!
  • @TomGray 我已经更新了我的代码以在单独的组中捕获一个或多个属性。标记的单个或多个属性将被捕获为单个字符串。为了避免这个问题继续扩大,如果您在处理属性方面需要帮助(在自己尝试之后),请提出一个新问题。
  • 我想问的最后一个问题是我如何从用户表单中获取输入,因为我现在可以访问我的笔记本电脑并尝试修改代码以从表单中获取 $UserInput用户输入他们的代码。
  • @TomGray 我正在假设您的表单正确地将数据发布到此代码。在这种情况下,您的问题中的if(isset($_POST['code'])){ $UserInput = htmlspecialchars($_POST['code']); 将是合适的。
  • 是的,我的表单应该包含在我的代码示例中,但是由于某种原因它没有包含在示例中。我只有一个简单的 HTML 表单,它自己使用 POST。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-11-04
  • 1970-01-01
  • 2020-12-13
  • 2018-08-06
  • 1970-01-01
  • 2014-01-24
  • 1970-01-01
相关资源
最近更新 更多