Page 1 of 1
checking for url and HTML
Posted: Thu Dec 17, 2009 6:44 pm
by kapil1089theking
How can I check out weather $POST['textfield'] has URL or HTML Code,
code will assign false if the string from textField contains URL or HTML else true.
So need to check out the presence of URL or HTML Code.
Re: checking for url and HTML
Posted: Thu Dec 17, 2009 7:30 pm
by dbsuk
Hi again,
You would need to use a regular expression (
http://www.regular-expressions.info/php.html).
The following function would do what you need.
Try....
Code: Select all
<?
function ContainsHtml ($var) {
if(ereg("(\]+)\>)", $var)) {
return true;
} else {
return false;
}
}
// you would then use it like this.
if(ContainsHtml($_POST['textarea'])) {
// warn the user that html is not allowed
echo 'Html is not allowed in the textarea';
} else {
// No html tags present, insert the data into the database.
}
?>
Regards
Richard
Re: checking for url and HTML
Posted: Thu Dec 17, 2009 7:55 pm
by kapil1089theking
how to check URL?
thanx for you lovely support again.
Re: checking for url and HTML
Posted: Thu Dec 17, 2009 8:13 pm
by dbsuk
I only read part of the question first time round.
Could not really find anything solid for detecting URLs with regular expressions, you need regexp guru.
However this would work:
Code: Select all
<?
function ContainsUrl($var) {
// array containing a list of filters, add more to this list
// I only thought of a few, you could add all TLD's if you like.
$filters = array('http://',
'www.',
'.com',
'.co.uk',
'.biz',
'.info',
'.name',
'.net',
'.org',
'.pro',
'.org'
);
// Cycle through the array of filters
foreach($filters as $filter) {
// check if the $var contains any of the filters.
if(strpos($var,$filter)!==false) {
return true;
}
}
// if none of the filters are found in $var, return false.
return false;
}
// To use it
if(ContainsUrl($_POST['textarea'])) {
echo 'URLS are not allowed';
} else {
// insert the data into the database.
}
?>
To use both functions together you could do:
Code: Select all
<?
// Use the || Logical Operator - meaning: OR
if(ContainsUrl($_POST['textarea']) || ContainsHtml($_POST['textarea'])) {
echo 'URLS and HTML tags are not allowed';
} else {
// insert the data into the database.
}
?>
Hopefully your well on your way now.
Best regards
Richard
Re: checking for url and HTML
Posted: Thu Dec 17, 2009 8:46 pm
by kapil1089theking
How to check blanks
I mean It will check wether any character other than blank is present in the text field or not just to ensure no blank string is entered into the table.
Re: checking for url and HTML
Posted: Thu Dec 17, 2009 9:12 pm
by kapil1089theking
thanx... Have used trim function...
Tons of thanx for ur help and support.