views:

67

answers:

4

How to take off 1 in "{1}" and use it?

+5  A: 
if (preg_match('/\{(\d+)\}/', $str, $mtch))
    echo $mtch[1];

where $str is '{1}'

by the way, you might wanna anchor that regex like this: /^\{(\d+)\}$/It will make sure the string contains exactly that and it won't match for, let's say 'abcd{4}'
A: 

You can use a preg_replace function to perform a regular expression.

What exactly do you need to do the string.

If you want to get a certain character of the string, $str = '{1}'; $str[1] will return the 2nd character of the string.

CodeJoust
A: 

Depends on what you actually have .. in the example above this would be enough:

$number = $string{1};

But i guess you need more something like

preg_match('/{([0-9]+)}/', $string, $matches);
$number = $matches[1];
johannes
+2  A: 

If you just want to get rid of the brackets, you may use trim() function

    $str = "{1}";
    $str = trim($str, "{}");
    echo $str;  //output: 1

EDIT: I removed the comma - "{}" is enough as the secont parameter for trim() (was "{,}" before the edit)

Vafliik