Skip to content

Instantly share code, notes, and snippets.

@abdullahbutt
Last active August 29, 2015 13:58
Show Gist options
  • Select an option

  • Save abdullahbutt/10253343 to your computer and use it in GitHub Desktop.

Select an option

Save abdullahbutt/10253343 to your computer and use it in GitHub Desktop.
get last word in URL and delete what is not necessary
get last word in URL and delete what is not necessary
if in $last_word you find also -0.htm then delete -0.htm
I explain my $last_word now shows test-0.htm
But I do not need -0.htm
I only need "test".
How do I say in PHP to delete -0.html and to grab only "test".
$last_word="-0.html";
$last_word = str_replace(array('-0.html','-0.htm'), '', $last_word);
It will replace the string (-0.html or -0.htm) even if found somewhere else than at the end of $last_word
Trying to remove -0.htm at the end of $last_word:
Regex is an easy way to go:
$last_word = preg_replace('/\-0\.htm$/', '', $last_word);
The $ means 'the end', which means the -0.htm has be be at the end.
If instead of -0.htm you want to remove -0.html:
$last_word = preg_replace('/\-0\.html$/', '', $last_word);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment