ereg
Moderator: General Moderators
ereg
I'm so confused about ereg/eregi_replace... How do I use it? What does all the [a-zA-z] mean? Anything else I need to know?
I use preg_replace and preg_match, but I think they're much the same
It's well worth leanring regular expressions. It may seem hard, but they're extremely powerful at manipulating text.
heres an example :
if you have a string s that could be any of the following and you want to censor it, this regular expression at the end works on all of these (excuse the swearing)
within the first part of preg_replace, you have '/..../' and replace a regular expression instead of ....
^ means start of the string
so this replaces a 'the' with '' (ie nothing) if the first 3 letters is 'the'
$s = preg_replace('/^the/','',$s)
$ means end of the string
so this replaces a 'this' with '' (ie nothing) if the last 4 letters is 'this'
$s = preg_replace('/this$/','',$s)
so this replaces a 'that' with 'none' if $s is 'that' with no other previous or trailing charactores at all
$s = preg_replace('/^that$/','none',$s)
anything in [] can be matched, so this will match the first a or b or c
$s = preg_match('/[abc]/',$s)
[a-z] is shorhand for [abcdef......xyz]
so [a-zA-Z] is shorthand for an alphabetic character
and [a-zA-Z0-9] is shorthand for an alphanumeric char
. represents a single charcter
* means zero or more of the previos char
+ means one or more of the previos char
so this matches one or more lower case chars
[a-z]+
\s means a blanck char (ie newline or space or tab, etc..)
\w means an alphanumeric (same as [a-zA-Z0-9])
\d means a digit
\S means non blank char
\W means non alphanumeric char
\D means non digit
so this means one or more spaces
\s+
this matches the first (space seperated) word
$str = preg_match('/\s*\S+\s*/', $s)
$s = "this is crazy";
this will get 0 or more spaces at the beginning (none here)
then and non space characters (here it is 'this')
so it will get "this"
It's well worth leanring regular expressions. It may seem hard, but they're extremely powerful at manipulating text.
heres an example :
if you have a string s that could be any of the following and you want to censor it, this regular expression at the end works on all of these (excuse the swearing)
Code: Select all
<?php
$s = "this is f_u_c_k_e_d";
$s = "this is <span style='color:blue' title='I'm naughty, are you naughty?'>smurf</span>";
$s = "this is f u c ke d";
$s = "this is f_u_c_k_e_d";
$s = "this is f-u-c-k-e-d";
$s = preg_replace('/[fF][\s-_]*[uU][\s-_]*[cC][\s-_]*[kK]/', "****", $s);
echo $s;
?>within the first part of preg_replace, you have '/..../' and replace a regular expression instead of ....
^ means start of the string
so this replaces a 'the' with '' (ie nothing) if the first 3 letters is 'the'
$s = preg_replace('/^the/','',$s)
$ means end of the string
so this replaces a 'this' with '' (ie nothing) if the last 4 letters is 'this'
$s = preg_replace('/this$/','',$s)
so this replaces a 'that' with 'none' if $s is 'that' with no other previous or trailing charactores at all
$s = preg_replace('/^that$/','none',$s)
anything in [] can be matched, so this will match the first a or b or c
$s = preg_match('/[abc]/',$s)
[a-z] is shorhand for [abcdef......xyz]
so [a-zA-Z] is shorthand for an alphabetic character
and [a-zA-Z0-9] is shorthand for an alphanumeric char
. represents a single charcter
* means zero or more of the previos char
+ means one or more of the previos char
so this matches one or more lower case chars
[a-z]+
\s means a blanck char (ie newline or space or tab, etc..)
\w means an alphanumeric (same as [a-zA-Z0-9])
\d means a digit
\S means non blank char
\W means non alphanumeric char
\D means non digit
so this means one or more spaces
\s+
this matches the first (space seperated) word
$str = preg_match('/\s*\S+\s*/', $s)
$s = "this is crazy";
this will get 0 or more spaces at the beginning (none here)
then and non space characters (here it is 'this')
so it will get "this"
So, let's say I've got a string for the name of a file. I want to see if the string has any wild charectors (ie: !@#$%^&*()_+=-[]\;'{}|:"<>?/.,). I would do an IF statement, using the preg_match() inside the IF, and if it finds any invalid characters, it'll give an error...
How would I go about doing such?
How would I go about doing such?
$find = array('!','@','#','$','%','^','&','*','(',')','_','+','=','-','[',']',''',';','''','{','}','|',':','"','<','>','?','/','.',',');
$cleaned = str_replace ($find,'',$dirty);
although with so many I don't know if it really is efficient, perhaps a
$cleaned = preg_replace('/[^A-z0-9]/','',$dirty);
may be faster? would be interresting to see a test/comparison..
$cleaned = str_replace ($find,'',$dirty);
although with so many I don't know if it really is efficient, perhaps a
$cleaned = preg_replace('/[^A-z0-9]/','',$dirty);
may be faster? would be interresting to see a test/comparison..
yea, I usually go for
if (preg_match('/[^a-zA-Z0-9-_]/',$dirty)) echo "invalid filename";
else echo "filename ok";
are there any other allowed characters other than
a-z A-Z 0-9 - _ ?
or as stoker said you could use :
$find = array('!','@','#','$','%','^','&','*','(',')','_','+','=','-','[',']',''',';','''','{','}','|',':','"','<','>','?','/','.',',');
$cleaned = str_replace ($find,'',$dirty);
if ($cleaned == $dirty) echo "filename ok";
else echo "invalid filename";
if (preg_match('/[^a-zA-Z0-9-_]/',$dirty)) echo "invalid filename";
else echo "filename ok";
are there any other allowed characters other than
a-z A-Z 0-9 - _ ?
or as stoker said you could use :
$find = array('!','@','#','$','%','^','&','*','(',')','_','+','=','-','[',']',''',';','''','{','}','|',':','"','<','>','?','/','.',',');
$cleaned = str_replace ($find,'',$dirty);
if ($cleaned == $dirty) echo "filename ok";
else echo "invalid filename";
matching patterns of unknown lengths and having character-classes, assertins and conditions are a strengths of (PC-) regular expressionsbut matching takes some time and patterns often are hard to read. Use str_... functions if a regex is unnecessary.
Code: Select all
<?php
$test = array('var="strings have to be quoted"', 'var=1234', 'var=test', 'var="1234"');
$pattern = '/([^=]*)=((?(?=")"\D+"|\d+))/';
foreach($test as $i)
{
echo '<pre>';
if (preg_match($pattern, $i, $matches))
print_r($matches);
else
echo "no match\n";
echo '</pre>';
}
?>Filename error chars: \ / : * ? " < > |
Usage:
Code: Select all
<?php
function clean_filename($file_name) {
// Set file error chars
$find = array('!','\'','/',':','*','?','"','<','>','|');
$cleaned = str_replace($find,'',$file_name);
if ($cleaned == $file_name) {
return TRUE;
} else {
return FALSE;
} // end if
} // end funtion
?>Code: Select all
<?php
if(clean_filename("num_pages.php")) {
echo "Successful file name.";
} else {
echo "Your file name has invalid characters.";
} // end if
// Successful file name.
if(clean_filename("num_pages?.php")) {
echo "Successful file name.";
} else {
echo "Your file name has invalid characters.";
} // end if
// Your file name has invalid characters.
?>