> The problem is that PHP produces error messages whenever I run this
> function. I thought it would be something basic, since I'm not
> familiar with the ereg_ functions.
Okay, what specifically are the errors?
> Anyway it should work but it doesn't. It doesn't matter that invalid
> characters exist, as long as I can remove them. This is a script that will
> run over a large bunch of files, and not really user feedback based.
It should *NOT* work if it doesn't. :) I know how frustrating these things
can be.
Keep in mind that ?, +, *, and | are special characters in REs and should be
preceeded with a backslash if you mean them in the literal sense. '?' means
"optional", '+' means "one or more", '*' means "zero or more", and '|' means
"OR".
Since the backslash character is always a little... quirky, you need to
escape it.
You have \\ in your code, which means it will be parsed as a single '\'.
But for some reason, you need to escape it _TWICE_. So your code should
look like this for it to work:
$name = ereg_replace("/","",$name);
$name = ereg_replace("\\\\","",$name);
$name = ereg_replace(":","",$name);
$name = ereg_replace("\*","",$name);
$name = ereg_replace("\?","",$name);
$name = ereg_replace("\"","",$name);
$name = ereg_replace("<","",$name);
$name = ereg_replace(">","",$name);
$name = ereg_replace("\|","",$name);
This works for me.
> I have a feeling I'm doing something not idiomatic here. I'm not too happy
> with the solution, I would like to find a cleaner solution. One
> that doesn't involve parsing the string char by char!
>
> I guess a switch case checking char by char would be easy and do the job,
> but I wanted a cleaner solution.
What you should do is set up a character class. What also works for me is
this (cleaner) solution:
$patt = "[/\\\\:\*\?\"<>\|]";
$name = ereg_replace($patt, "", $name);
Hope it helps,
Nik
---
NEED TECHNICAL TIPS, TOOLS, AND INSIGHTS? Is FREE okay?
Visit EarthWeb for the latest in IT Management, Software Development,
Web Development, Networking & Communications, and Hardware & Systems.
Click on http://www.earthweb.com for FREE articles, tutorials,
and discussions from the experts.
---
You are currently subscribed to pro_php as: $subst('Recip.EmailAddr')
To unsubscribe send a blank email to leave-pro_php-$subst('Recip.MemberIDChar')@p2p.wrox.com