How to Scan PHP Variables To Retrieve a Particular Value

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • abhighat4214
    New Member
    • Jul 2008
    • 13

    How to Scan PHP Variables To Retrieve a Particular Value

    Hello all.
    I wanted to ask a simple doubt. I am not so well versed with the syntax and semantics of PHP and I'm new to it.
    I'm having the following code.
    $var1="100 <" or $var1 = "> 100"
    What I want to do is segragate the relational operator and the number into 2 separate variables.The relational operator can be on the left side of or on the right side of the number(there is no hard and fast rule regarding the position of it).Kindly guide me on how it's supposed to be done.

    Thanking You all for your help
  • pbmods
    Recognized Expert Expert
    • Apr 2007
    • 5821

    #2
    Heya, Abhighat.

    To do this, you'll want to use regular expressions (http://regular-expressions.info):

    [code=php]
    preg_match('/(>?)(\\d+)(>?)/', $var, $matches);

    $number = $matches[2];
    $operator = (empty($matches[1]) ? $matches[3] : $matches[1]);
    [/code]

    Comment

    Working...