ereg

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
phice
Moderator
Posts: 1416
Joined: Sat Apr 20, 2002 3:14 pm
Location: Dallas, TX
Contact:

ereg

Post by phice »

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?
Image Image
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

Me too. So I use substr_replace() instead. I think it's quicker too.
User avatar
lazy_yogi
Forum Contributor
Posts: 243
Joined: Fri Jan 24, 2003 3:27 am

Post by lazy_yogi »

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)

Code: Select all

<?php
$s = "this is f_u_c_k_e_d";
$s = "this is <span style='color:blue' title='I&#39;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"
User avatar
Stoker
Forum Regular
Posts: 782
Joined: Thu Jan 23, 2003 9:45 pm
Location: SWNY
Contact:

Post by Stoker »

The world is not simple, but a simple rule regarding any string search, match or replace operations:

Never use ereg (posix), always use native/simpe text/string functions if you can (like str_replace etc), if you really need regular expression power, use preg
User avatar
phice
Moderator
Posts: 1416
Joined: Sat Apr 20, 2002 3:14 pm
Location: Dallas, TX
Contact:

Post by phice »

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?
Image Image
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

Thanks for the info.
User avatar
Stoker
Forum Regular
Posts: 782
Joined: Thu Jan 23, 2003 9:45 pm
Location: SWNY
Contact:

Post by Stoker »

$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..
User avatar
lazy_yogi
Forum Contributor
Posts: 243
Joined: Fri Jan 24, 2003 3:27 am

Post by lazy_yogi »

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";
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

matching patterns of unknown lengths and having character-classes, assertins and conditions are a strengths of (PC-) regular expressions

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>';
}
?>
but matching takes some time and patterns often are hard to read. Use str_... functions if a regex is unnecessary.
User avatar
phice
Moderator
Posts: 1416
Joined: Sat Apr 20, 2002 3:14 pm
Location: Dallas, TX
Contact:

Post by phice »

Filename error chars: \ / : * ? " < > |

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
?>
Usage:

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.


?>
Image Image
Post Reply