Reputation: 289
I use $_SERVER['REQUEST_URI']
to get this /res/about_us.php
, I want the output to be About Us
, I know I can use preg_replace
for this, it is the regular expression I am struggling with.
I have this pitiful attempt thus far:
echo str_replace('.php', '', $uri);
I realised after this that I cant replace two cases using str_replace... So I thought I need preg_replace with regex, but not a clue how it works, cant seem to find a similar example through Google.
Regards
Upvotes: 2
Views: 129
Reputation: 6199
Actually you can replace multiple cases with str_replace
by passing an array of things you'd like to replace as first parameter of the function! Something like this:
echo ucwords(str_replace(array('_', '-', '.php'), ' ', basename($uri)));
Inside the array you could put whatever you'd like to replace.
Upvotes: 0
Reputation: 13476
Without regular expressions you can do:
// Get the filename, e.g., 'about_us'.
$filename = pathinfo($_SERVER['REQUEST_URI'], PATHINFO_FILENAME);
// Replace underscore with spaces and capitalise words
$title = ucwords(str_replace('_', ' ', $filename));
Upvotes: 1