0

Let say I have a string like this

sometext<br>sometext<br>sometext(Rating 50)<br>sometext<br>

All I want to get from this string is the "50" right after the "Rating". The number can range from 1 to 3 digits long.

Can I use something like preg_replace? explode? not sure how to get it.

1
  • preg_replace('/\(Rating (\d+)\)/','$1',$thestring); Commented Jan 5, 2012 at 7:52

2 Answers 2

3
$str = "sometext<br>sometext<br>sometext(Rating 50)<br>sometext<br>";
preg_match('/\(Rating (\d+)\)/',$str, $matches);
echo $matches[1];
Sign up to request clarification or add additional context in comments.

1 Comment

If Sudhir's answer worked for you consider accepting it via the tick left of the answer. This way, people are more likely to answer your future questions.
0

You can use explode to get individual components and then search for "Rating". Then 50 will be the element after "Rating". Of course, if you are going to use similar routine frequently, it's better to make a helper function that you could use repeatedly.

But be aware that, this will only work for the first occurrence of the string you are looking for.

Perhaps, there is an elegant solution involving Regular Expressions as opposed to creating an intermediate array and looping.

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.