0

Great guys have helped me a lot in this other post:

Get last word from URL after a slash in PHP

Now I am facing another issue.

How can I say something like this in PHP:

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".

Thanks for your help. Since it is a dynamic script I obviously do not know what is before "-0.html". The word "test" is only an example. Just to let you know that "test" is represented by a variable in my code, and it works. Now I only need to tell to the code to eliminate 0.html when is found.

THANK YOU

Community
  • 1
  • 1
DiegoP.
  • 45,177
  • 34
  • 89
  • 107

2 Answers2

2
$last_word = str_replace(array('-0.html','-0.htm'), '', $last_word);

Hope this helps. It will replace the string (-0.html or -0.htm) even if found somewhere else than at the end of $last_word.

Tadeck
  • 132,510
  • 28
  • 152
  • 198
  • YES THATNKS IT WORKS. NOW I GOT YOUR POINTS, BOTH YOURS AND THE RUDIE'S ONE. SORRY GUYS, I AM NEW TO PHP. I KNOW I SEEMED IDIOT. I WAS JUST COMPLETELY CONFUSED. – DiegoP. May 07 '11 at 14:26
1

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);
Rudie
  • 52,220
  • 42
  • 131
  • 173
  • ok but how can I say to the code if -0.htm is found at the end? – DiegoP. May 07 '11 at 13:54
  • @DiegoP: Both of these solutions will do that, just in different ways. – Wesley Murch May 07 '11 at 13:57
  • No wait. Maybe I have not explained it well. I need to say to the code that if in $last_word is found -0.htm at the end, then delete 0.htm and leave only "test". To grab "test" I used the function provided on the other post, that is: preg_match("/[^\/]+$/", "http://www.mydomainname.com/m/groups/view/test", $matches); $last_word = $matches[0]; // test – DiegoP. May 07 '11 at 14:00
  • Just to be clear I use a variable. The function I use is:preg_match("/[^\/]+$/", $_SERVER['REQUEST_URI'], $matches); $last_word = $matches[0]; // test – DiegoP. May 07 '11 at 14:01
  • You want `-0.html` removed from a string where it definitely not exists in?? That's strange... You say `"I need to say to the code that if in $last_word is found -0.htm at the end, then delete 0.htm and leave only "test""` which is exactly what my answer does =) – Rudie May 07 '11 at 14:02
  • Ok I will try to explain as better as I can. I use this function: preg_match("/[^\/]+$/", $_SERVER['REQUEST_URI'], $matches); $last_word = $matches[0]; // test . This function in some cases gives http://www.iamassyrian.com/m/groups/view/test , in some other cases it gives http://www.iamassyrian.com/m/groups/view/test-0.htm. So I have to say to the code that if you find -0.htm, then delete 0.htm and give me only http://www.iamassyrian.com/m/groups/view/test. But "test" is the result of a variable, this is why I do not know to say to the code "if you find -0.htm after that variable =(( – DiegoP. May 07 '11 at 14:04
  • I understand how you get `$last_word`. The problem is that sometimes `$last_word` contains `"-0.html"`, right? So you want that removed? That's what the codes in my answer do. **edit** Just try it and see if it is what you mean =) – Rudie May 07 '11 at 14:05
  • Wow... You have now explained the same thing four times and four times everybody understood and verified to you that my answer does exactly what you want. What else do you need??? – Rudie May 07 '11 at 14:08
  • ok WAIT. Maybe I found a good way to explain. This is the logic of the code. If in $last_word you find 0.htm, then delete 0.htm and leave all the rest. Is this clear now? How do I achieve this? I am confused. – DiegoP. May 07 '11 at 14:09
  • Yeah so am I. I'm also annoyed. I wanna use caps, but I won't =) **Please** try out my answer. It'll do exactly as you want. How can you not understand that I understand what you mean? It's not so hard... If the last part of `$last_word` is `-0.htm`, remove it! – Rudie May 07 '11 at 14:13
  • I've updated `$url` to `$last_word` in my answer. Do you get it now? – Rudie May 07 '11 at 14:13
  • Ok Maybe I am an idiot. This is what I am doing preg_match("/[^\/]+$/", $_SERVER['REQUEST_URI'], $matches); $last_word = $matches[0]; // test $last_word = preg_replace('/\-0\.htm$/', '', $last_word); $last_word = substr($last_word, 0, -6); This is completely deleting "test" and it is leaving only -0.htm. This is doing exactly the opposite of what I am trying to achieve. Then I have to say to the code "if -0.htm is found", because -0.htm it's not ALWAYS there. Only when is found must be deleted. – DiegoP. May 07 '11 at 14:16
  • Rudie. Really you are GREAT. I understand your point. Completely. And it works fine if it was what I am looking for. But this is not what I am looking for so far. I mean I need a function that will say "if in $last_word you find -0.htm, then delete -0.htm". You have provided the function to correctly delete -0.htm. What is missing is "if is found", because if I do not give an if, the function you have provided will delete 6 characters even if when -0.htm it's not there. Do you know how can I say "if -0.htm is in the variable $last_word then delete it?" – DiegoP. May 07 '11 at 14:24
  • **OMG this is not worth it**. I give up. Just use Tadeck's answer. – Rudie May 07 '11 at 14:27