我正在開發一個使用存儲在數組中的HTML有效標記的網站,以便與輸入到系統中的用戶進行比較。我目前開發了包含我需要的所有HTML標籤的數組。我還開發了一個函數,根據存儲在數組中的數據驗證用戶輸入。在我的數組中搜索包含特定字符的值
如果它是有效的,那麼它會告訴用戶它已被接受,並且有效標籤將被放入包含用戶輸入的所有標籤的不同數組中。
但是我試圖創建這樣的功能,即當用戶在系統內輸入標籤時,將搜索包含反斜槓/的任何標籤的數組,因爲這將標識數組內的所有結束標籤。
所以我的問題是我將如何提取我的AllowedTags數組中包含/字符的所有記錄,並將它們存儲在新數組中。
<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("<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(/)
$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>
設置'$ ret'的代碼有什麼問題? – Barmar
你應該使用array_filter()函數。 –
你爲什麼在'array_filter()'的結果上使用'array_keys()'?如果你想要標籤列表,'array_filter()'返回它。 – Barmar