Tom Gray
Tom Gray

Reputation: 15

Searching my Array for values containing a specific character

I am developing a Website that is using HTML Valid Tags stored within an array for comparison against the users input into the system. I have currently developed the Array which contains all the of the HTML Tags that i require. I have also developed a function which validates the user input against data that is stored within the array.

If it is valid then it will tell the user it has been accepted and the valid tag will then be put in a different array which contains all of the tags that the user has inputted.

However i have tried to create the functionality that when the user inputs the tags within the system the array will be searched for any tags that contain a backslash / as this will identify all of the closing tags within the array.

So my question would be is how i would extract all records within my AllowedTags Array that contain a / Character and store those within a new array.

<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>

Upvotes: 0

Views: 66

Answers (2)

shiroyasha
shiroyasha

Reputation: 1

If the user is providing the tags without angular brackets and only writing the tag name (say "head"), then maybe you can concatenate a backslash() in the beginning of the user input string while searching for the closing tag in the $AllowedTags. I'm sorry if I misunderstood the question.

Upvotes: 0

Alex Myznikov
Alex Myznikov

Reputation: 969

"how i would extract all records within my AllowedTags Array that contain a / Character and store those within a new array"

   $filtered_tags = array_filter($AllowedTags, function($val) {
      return (bool)preg_match('/\//', $val);
   });

   print_r($filtered_tags);

Upvotes: 1

Related Questions