Filtering contents of an HTML input element using a server-side script
This php script remove all <a></a>
tags
i want placed this php script in html form when i click remove button of form they remove all <a></a>
tags
please any expert convert this script in form
<?php
$str = 'I would like <a href="http://www.google.com">google</a> this link.';
$filter = preg_replace("/<a(.*)<\/a>/iUs", "", $str);
print $filter;
?>
i want use this form
<form>
<text开发者_Go百科area name="" style="width: 400px; height: 100px"></textarea>
<input type="button" value="Remove all <a></a>" onClick="">
</form>
You mean
<script type="javascript">
function removeAll(theForm) {
var str = theForm.text.value;
if (str) theForm.text.value = str.replace(/<a(.*)<\/a>/ig,"");
}
</script>
<form>
<textarea name="text" style="width: 400px; height: 100px"></textarea>
<input type="button" value="Remove all anchors" onclick="removeAll(this.form)" />
</form>
or
<script type="javascript">
function validate(theForm) {
var str = theForm.text.value;
if (str && str.match(/<a(.*)<\/a>/ig)) {
alert('Please do not enter any links')
theForm.text.focus();
return false;
}
return true;
}
</script>
<form method="post" action="" onsubmit="return validate(this)">
<textarea name="text" style="width: 400px; height: 100px"></textarea>
<input type="submit" name="submit" value="Send to server" />
</form>
You still need to test on the server since JS could be turned off
It looks like you're trying to filter HTML with a regex.
Please don't.
Take a look at HTML Purifier, a comprehensive HTML filtering mechanism in PHP. Using it, you can ensure that the HTML your users are submitting fits a particular set of guidelines, like ensuring there are no anchor tags.
精彩评论