0

I am making application where I receive a string from user. The string is concatenated with - character between them. First part of string contains alphabetic data whereas later part contains integers or floating point numbers. For example: A string might be 3 Cups Tea-5.99.I want to get the later part of string 5.99 separated by - character. How to do that? I know about PHP substr() function but that takes fixed characters to retrieve substring from. But in this case the later part will not be fixed. For example: 2 Jeans-65.99. In this case I would need last 4 characters meaning that I can't use substr() function. Anybody with solution? I know I would need to apply regex but I am completely novice in Regex. Waiting for your help. Thanks!

3
  • please, stop and think about what you're doing. Are you absolutely sure that sending around strings separated by - are your best tool for the job? what if you get a string like 3 kit-kat tablets-12.00? have you ever heard of JSON? Commented Oct 16, 2013 at 11:51
  • Yeah. You are right, thought if this, that is why I asked I want to take the string from end to the point where first occurance of "-" takes place. Heard of JSON but I am to PHP only. What would be the solution then if user enters 3 kit-kat tablets-12.00? Commented Oct 16, 2013 at 11:55
  • JSON isn't relegated to javascript only. it has plenty of use inside PHP too, that's why we have json_encode() and json_decode(). Also, the very fact that the user has to manually type 3 kit-kat tablets-12.00 is a bad design. You need to have 3 different input fields (quantity, product and price) and then send them through a form. HTTP then will properly handle the transmission of these values and you won't need to do any string manipulation. Commented Oct 16, 2013 at 12:06

4 Answers 4

3

Simply

$result = explode('-', $string)[1];

For PHP<5.4 you'll have to use temporary variable:

$data = explode('-', $string);
$result = $data[1];

Edit

As mentioned in comments, if there is more than 1 part, that will be:

$result = array_pop(explode('-', $string));
Sign up to request clarification or add additional context in comments.

7 Comments

Not sure that would work. Explode takes the delimiter first, afaik ..?
That's all about mess with implode/explode .. thank you (first one accepts args in any order)
If the input can contain more then one dash use end() instead of hardcoding it to the second part.
@Darsstar fair enough. But end() is not needed since array_pop() exists
@AlmaDoMundo Thank you very much for your answer as it handles the possible scenario of - occuring more than once.
|
1
$bits = explode('-', $inputstring);
echo $bits[1];

Comments

1

You can use substr() with strpos():

$str = '3 Cups Tea-5.99';
echo substr($str, strpos($str, "-") + 1);

Output:

5.99

Demo!

1 Comment

@user2885137: No worries :)
0

If data will be like this: "1-Cup tea-2.99", then

$data = "1-Cup tea-2.99";
$data = explode('-', $string);
$result = $data[count($data)-1];

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.