$titleadd = substr($_SERVER["SCRIPT_NAME"],strrpos($_SERVER["SCRIPT_NAME"],"/")+1);
$titleadd = str_replace('.php', '', $titleadd);
$titleadd = str_replace('-', ' ', $titleadd);
I’m getting the url, removing the http://www.domain.com/ then replacing the .php and any hyphens in the url to echo out the page name.
So http://www.google.com/my-page.php would output as my page
Is there a shorter way of doing this?
A shorter equivalent for your use case is
Of course this is not industrial-grade parsing, but it should give as good a result as your original code.
A more robust attempt would involve
parse_url, but it will hardly end up being shorter than what you have there. On the other hand: perhaps code length is not the correct metric here? After all, you can hide 200 lines of code behindfoo()and noone will ever know.