Web Hosting Talk







View Full Version : Removing Underscore w/PHP


P-nut
11-20-2005, 09:49 PM
OK I know I know how to do this but for the life of me can't remember nor find my answer here or on Google.

I have website pages that use PHP to read the file name (before the .php) and use that to generate the page title on the fly.

Example: Compare-Plans-Now.php = Title of 'Compare-Plans-Now'

Is it possible to use PHP to replace the dashes between the words so that 'Compare-Plans-Now' becomes 'Compare Plans Now'? If so, how would I do that?

Thanks! :D

IPsecure
11-20-2005, 09:57 PM
You can use preg_replace.

Here's the link from php.net.

http://us2.php.net/preg-replace

I used this once awhile back to replace whitespace with an underscore.

probonic
11-20-2005, 10:44 PM
It would actually be more efficient to simply use str_replace for this, as you are only replacing simple strings. Simply use something like:

$title = str_replace('-',' ',$title);

IPsecure
11-20-2005, 11:22 PM
LOL you made me go back and look at my code to see exactly what i used. :S I did use str_replace to replace my white space with underscores. Don't know why preg_replace was the first thing to pop into my head.