-4

So i have variable

$var = "how-to-stack-overflow"

I want to ECHO "overflow", or the last word from the string upto a hyphen.

Please show any method to accomplish this, I know this question may have duplicates, but i am a newbie and can't figure out how to put regex expressions using PHP(many valid answers are just the regex without any php), Thanks a lot in advance :)

PLEASE ONLY SHOW USING NEWBIE PROOF PHP

Subrata
  • 37
  • 1
  • 8
  • http://php.net/manual/en/function.preg-match.php but actually http://php.net/manual/en/function.explode.php instead – Sammitch Jul 06 '18 at 23:41
  • THAT, is chinese for me, please understand i am a newbie and i already read that before posting this question. – Subrata Jul 06 '18 at 23:49
  • http://php.net/manual/en/langref.php – Sammitch Jul 06 '18 at 23:57
  • lol i didnt mean literally, sorry, i mean that i am not able to understand anything out of php docs, though i have found my answer, ty – Subrata Jul 06 '18 at 23:59
  • What have you tried so far? Based on your tone, it feels like that you just would like to get the answer for a homework, but I may be wrong. Please take a look at [how to be nice on Stackoverflow](https://stackoverflow.com/help/be-nice) and prove it that you have been trying to solve your problem. – toraritte Jul 07 '18 at 00:01

2 Answers2

2

/\w+$/

grab one or more word characters up to the end of the string.

Demo: https://regex101.com/r/onEfF8/1

Edit: Example in php

$var = "how-to-stack-overflow";
$regex = preg_match('/\w+$/', $var, $match);
$match = $match[0];

echo $match;
Jonathan
  • 250
  • 1
  • 8
0
$var = "how-to-stack-overflow";
$explode=explode('-',$var);
$count=count($explode);
echo $explode[$count - 1];
  • 1
    While this code snippet may be the solution, [including an explanation](https://meta.stackexchange.com/questions/114762/explaining-entirely-%E2%80%8C%E2%80%8Bcode-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. – Narendra Jadhav Jul 07 '18 at 05:05