2

I am trying to get the ID from the end of this string the 4305 after the -. The code below works from left to right and shows 150. How can i make it work from right to left to show the 4305 after the -?

  $mystring = "150-Adelaide-Street-Brisbane-Cbd-4305";
  $mystring = substr($mystring, 0, strpos($mystring, "-"));
  echo $mystring;

Updated: This does what i need but i'm sure there is a better way to write it:

  $mystring = "150-Adelaide-Street-Brisbane-Cbd-4305";
  $mystring = substr(strrev($mystring), 0, strpos(strrev($mystring), "-"));
  echo strrev($mystring);
3
  • Instead of strpos-forward, look for strpos-backward .. (or apply a reverse transformation or split or ..) Commented Jul 18, 2012 at 2:14
  • substr($mystring, strrpos($mystring, "-") + 1) Commented Jul 18, 2012 at 2:17
  • ^ this is the most proper way - even if there's several possibilities. Commented Jan 1, 2013 at 23:44

5 Answers 5

5

You can use strrpos to get the last hyphen in the string, and then take the rest of the string after this character.

$mystring = "150-Adelaide-Street-Brisbane-Cbd-4305";
$mystring = substr($mystring, strrpos($mystring, "-") + 1);
echo $mystring;
Sign up to request clarification or add additional context in comments.

5 Comments

Ok, now tell me How easy will it be, if you want to extract Street out of the string above? You will probably write another similar line of code, right? Now if you want to call that is efficient, then I have nothing to say. But yes, if it is about just one last word. This is good.
What does street have to do with the price of corn in China? That wasn't the question trying to get street. He wants to get the ID at the end
@Kris, If you are going to go literally, the question is about getting the string right to left, ha ha. :) (Cheers)
@Starx I'm trying to answer the question posed. If the question was to parse each piece of the string, I would have posted a different answer. I agree that if more pieces of the string are needed, this isn't the best solution.
Yes, i was just after the ending ID glad you guys understood it :)
4

Split it on the - into an array, and pop off the last element:

$mystring = "150-Adelaide-Street-Brisbane-Cbd-4305";
$parts = explode("-", $mystring);
echo array_pop($parts);

Or, if you need to keep the array intact, use end() to advance to the last element and return it:

echo end($parts));

This is less efficient than using strrpos(), but in cases when that isn't an issue I'll tend to use this method since it doesn't require me to think much about string indices and if what you need isn't in the first or last delimited position, it's a bit easier to pull an array index than to do the string manipulation needed to pull from the internal parts.

3 Comments

There's no need to split up the entire string.
I agree, explode function seems like the best solution. I think he wants to get the ID from the string so using the end() function would be the function to use.
Definitely less prone to errors with the position. I used to retrieve last element with $parts[sizeof($parts)-1] ... end() function was new to me in this context, have to remember that.
2

If your like one of my coworkers who likes to use regex for EVERYTHING... then

  $mystring = "150-Adelaide-Street-Brisbane-Cbd-4305";
  preg_match('~-(\d+)$~', $mystring, $matches);
  echo $matches[1];

2 Comments

array / string functions might perform faster (not bench-marked in detail - but have a feeling). The required matching is way to simple that it would require regex '/.*-(\d+)$/' would rather match the requirements - that's several digits from the end of the string. Voted up for a possible solution - but please update the pattern, tested it and the match fails after step 1).
Get your coworker one of these: regexbuddy.com. Guess he/she would completely go nuts with it, it's a great tool for building up more complex expressions and for testing their performance.
0

Most easiest way is definitely using explode.

By using explode, you can split the string into an array with each parts accessible as individual identifiers using the indexes.

Usage Example:

  $mystring = "150-Adelaide-Street-Brisbane-Cbd-4305";
  $mystring = explode("-", $mystring);
  echo $mystring[count($mystring)-1]; //Extract the last item

1 Comment

I wouldn't say this is the easiest and certainly not the most efficient.
0

As Matthew posted in comments above, reverse scan is faster since the match is done after 4 steps only - guess there's no "more proper way" to translate the requirements into PHP syntax (this is also the way how they teached me when I learnt C syntax):

echo substr($mystring, strrpos($mystring, "-") + 1);

PHP manual.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.