在我的Array中搜索包含特定字符的值

时间:2017-02-14 17:12:04

标签: php html

我正在开发一个使用存储在数组中的HTML有效标记的网站,以便与输入系统的用户进行比较。我目前开发了包含我需要的所有HTML标签的数组。我还开发了一个函数,它根据存储在数组中的数据验证用户输入。

如果它有效,那么它将告诉用户它已被接受,然后有效标签将被放入一个包含用户输入的所有标签的不同数组中。

但是我试图创建一种功能,当用户在系统中输入标签时,将搜索数组中包含反斜杠/的任何标签,因为这将识别数组中的所有结束标签。

所以我的问题是如何在AllowedTags数组中提取包含/ Character的所有记录并将它们存储在新数组中。

<html>
<head>
</head>
<body>
<form id="HTMLValidation" method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
<p> HTML Code: <input type="text" name="code">
<p><input type="submit" name="submit" value="Validate Your Code!"/></p>
</form>
<?php
//Depricated
//$TagArray = $UserInput.split("");
if(isset($_POST['code']))
    {
        $UserInput = htmlspecialchars($_POST['code']);
        $InputtedTags = 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(/)
        $CloseTags = implode(" ",$AllowedTags);
        $needle = '/';
        $ret = array_keys(array_filter($AllowedTags, function($var) use ($needle){
                    return strpos($var, $needle) !== false;}));
        print_r($ret);

        //Check What The User Has Inputted Into The System against the AllowedTags Array 
        //If it is true then display to the user the tag is valid
        //Push The value that the user entered onto the InputtedTags Array
        foreach($AllowedTags as $data)
            {

                if(strpos($UserInput,$data) !==false )
                    {
                        echo($UserInput. ": Valid Tags");
                        array_push($InputtedTags,$UserInput);
                    }

            }
        print_r($InputtedTags);
    }
?>
</body>
</html>

2 个答案:

答案 0 :(得分:1)

“我将如何提取AllowedTags数组中包含/ Character的所有记录并将其存储在新数组中”

grep "cmt /tm9" output_file | cut -d'/' -f2,3,9,10 | cut -d '.' -f1 | uniq

答案 1 :(得分:0)

如果用户提供没有尖括号的标签并且只写了标签名称(比如“head”),那么也许你可以在搜索结束标签时在用户输入字符串的开头连接一个反斜杠() $ AllowedTags。 如果我误解了这个问题,我很抱歉。