Wednesday, 2 October 2013

Manipulating string using PHP

Manipulating string using PHP

In PHP, I have strings like so:
$string = "This is a 123 test (your) string (for example 1234) to test.";
From that string, I'd like to get the words inside the () with the
numbers. I've tried using explode but since I have 2 word/group of strings
enclosed in the parentheses, I end up getting (your) instead of (for
example 1234). I've also used substr like so:
substr($string, -20)
This works most of the time but the problem with this is, there are
instances that the string is shorter so it ends up getting even the
unwanted string. I've also tried using regular expression in which I set
something like so:
/[^for]/
but that did not work either. The string I want to get always starts with
"for" but the length varies. How do I manipulate php so that I can get
only the string enclosed inside the parentheses that starts with the word
for?

No comments:

Post a Comment