我遇到的问题是从已包含信息的数组中找到用户在网站前端所做的具体输入。
正如当前系统所示,如果一次输入一个项目,它将从数组中提取项目,但是当我组合输入时,它会检测输入两次,而不是将输入分成两个不同的数组。
我认为这是因为我使用的是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("<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>","</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>");
//$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);
}
答案 0 :(得分:0)
现在使用更小的验证数组,更少的循环/过滤,没有strpos,没有array_push。 (您的$AllowedTags
个元素存在不一致性:一些元素带有>
,另一些元素带有>
)这样更精简,更清洁:
查看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="<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>";
// <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('/<([^\/][a-z1-6]*)\s?(.*?)>|<\/([^&]*?)>/',$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
中所有有效和无效的开始和结束标记。根据您的确切需求修改我的解决方案如有必要,我很乐意进一步解释。
*请注意,如果您的程序允许包含属性的标记,则需要扩展正则表达式模式以允许(而不是捕获)属性。