Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Jul 2002
    Location
    New Zealand
    Posts
    127
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Help With Regular Expressions

    I'm having trouble working with regular expresions.

    What I want to do is grab a line of text from a string. The string is taken from a text file with line breaks... something like this:

    Code:
    $data="//blah blah
           VALUE_ONE=0.00001
           VALUE_TWO=0.00002
           VALUE_THREE=0.00003
           //blah blah"
    Now, the variable $row_id="VALUE_ONE=" is specified by the user, what I want to do is grab the value after the coresponding line from the $data string. In this case, me desired result is 0.00001

    Can someone help me out here, I only know the very basics of regular expressions but I presume I could use ereg_replace() to remove all the text before $row_id and all the text after the $row_id line... or something...

    Thanks for your help.
    eTheory - the theory of revolution

  • #2
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,040
    Thanks
    10
    Thanked 92 Times in 90 Posts
    I would not regex unless I had to... (good job as well cos I hate it)
    PHP Code:
    <?
    $key 
    'VALUE_ONE' ;
    $yaks file'data.txt' ) ;
        foreach( 
    $yaks as $line ){
            
    $bits explode'=' $line ) ;
            if( 
    trim$bits[0] ) == $key ){
                  
    $value $bits[1] ;
                  break ;
            }
        }

    echo 
    $value;
    ?>
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #3
    Regular Coder
    Join Date
    Jun 2002
    Location
    Sheffield, UK
    Posts
    552
    Thanks
    0
    Thanked 0 Times in 0 Posts
    just out of interest, why is it you're so against regexp? i use them all the time, possibly too much actually, but i've never had any problems with it.
    "To be successful in IT you don't need to know everything - just where to find it in under 30 seconds"

    (Me Me Me Me Me Me Me Me Me)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •