Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Creating syntactically correct filenames

Status
Not open for further replies.

bentleykfrog

Programmer
Joined
Sep 30, 2005
Messages
1
Location
AU
Ok, I've got these title strings for products, ie:

"Australian Parti - Colour Sapphire"
"Tapered Parti-Colour Bagette Sapphire"
"Princess Cut 0.25carat"
etc...

and I want to turn these strings into syntactically correct filename strings, but I'm not exactly sure what is acceptable/not acceptable as a filename? All I know is that google uses the character "-" as a seperator for words, not "_".

I've seen blogspot do this for specific journal entries, so I know its possible.

Does anybody know how to do this correctly?

thanks
-bentley
 
Well, what counts as a "syntactically correct filename" depends on your platform. For example, UNIX-like systems allow some characters that are not allowed on Windows. Also, Windows paths are not case-sensitive, while UNIX paths are.

The easy way to do this is with a simple regular expression to strip out anything that might not be acceptable in a path. The exact substitution isn't really important, so long as it isn't likely to fail given your data. For example, it doesn't matter if you separate words by dashes or underscores - they're both valid in a file path for every filesystem I know of.

Here's a simple example for you:
Code:
$str = "Tapered Parti-Colour Bagette Sapphire";
$str = strtolower($str);
$str = preg_replace("/[^\w\.-]/", "_", $str);
This will convert the string to lower case and convert everything that's not a "word" character (i.e. a letter, digit, or underscore), a dot, or a dash into an underscore, giving the output "tapered_parti-colour_bagette_sapphire". The benefit of stripping non-"word" characters is that it should work on pretty much any platform you're likely to be using.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top