Jump to content


Photo

parsing text


  • Please log in to reply
4 replies to this topic

#1 xrado

xrado
  • New Members
  • Pip
  • Newbie
  • 5 posts

Posted 26 August 2006 - 12:20 PM

i would like to parse string:
apple(3),banana(10),orange(aaa)

and get out the fruit name and what ever is between ()

Array
(
    [0] => Array
        (
            [0] => apple
            [1] => 3
        )
    [1] => Array
        (
            [0] => banana
            [1] => 4
        )
    [2] => Array
        (
            [0] => orange
            [1] => aaa
        )
)

i would also like to still get the name if () is missing ..like this [move][move][/move][/move]

    [2] => Array
        (
            [0] => orange
            [1] => 
        )

...this is what i achomplished till now with

$aa = "apple(3),banana(10),orange(aaa)";
preg_match_all("/([a-zA-Z0-9 ]*)(\(?([a-zA-Z0-9]*)\)?)/", $aa, $matches, PREG_SET_ORDER);
print_r($matches);

Array
(
    [0] => Array
        (
            [0] => apple(3)
            [1] => apple
            [2] => (3)
            [3] => 3
        )

    [1] => Array
        (
            [0] => 
            [1] => 
            [2] => 
            [3] => 
        )

    [2] => Array
        (
            [0] => banana(10)
            [1] => banana
            [2] => (10)
            [3] => 10
        )

    [3] => Array
        (
            [0] => 
            [1] => 
            [2] => 
            [3] => 
        )

    [4] => Array
        (
            [0] => orange(aaa)
            [1] => orange
            [2] => (aaa)
            [3] => aaa
        )

    [5] => Array
        (
            [0] => 
            [1] => 
            [2] => 
            [3] => 
        )

)

if i get four dimensional resoult its ok..but i dont want empty result

#2 rea|and

rea|and
  • Members
  • PipPipPip
  • Advanced Member
  • 32 posts

Posted 26 August 2006 - 03:18 PM

Try this one

$aa = "apple(3),banana,orange(aaa),pineapple";
preg_match_all( '/(\w+)(?:\((\w*)\))?/',$aa,$mth,PREG_SET_ORDER ) ;



#3 xrado

xrado
  • New Members
  • Pip
  • Newbie
  • 5 posts

Posted 26 August 2006 - 04:11 PM

thx!  :)

\w is for word.. but what if is want to say everything except ","

i would also like to have space and some other characters befour and inside the ()

#4 rea|and

rea|and
  • Members
  • PipPipPip
  • Advanced Member
  • 32 posts

Posted 26 August 2006 - 11:50 PM

Well, if you want to catch everything but commas you define a class of chars that has only the comma within and deny it [^,]. You could use something similar to match everything (white spaces included) inside the brakets [^)] and outside [^,)]. Something like this it'd have to work:

$aa = "apple(3),ban ana ,ora ng e( a a a),pineapple";
preg_match_all( '/([^(,]+)\s*(?:\(([^)]*)\))?/',$aa,$mth,PREG_SET_ORDER ) ;



#5 xrado

xrado
  • New Members
  • Pip
  • Newbie
  • 5 posts

Posted 27 August 2006 - 08:47 AM

thx a lot! works just like i wannet  :)
i have to learn those regex exprassions




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users