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