...

View Full Version : Help With Regular Expressions



Candrias77
11-11-2003, 03:48 AM
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:


$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.

firepages
11-14-2003, 10:46 AM
I would not regex unless I had to... (good job as well cos I hate it)


<?
$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;
?>

ReadMe.txt
11-16-2003, 11:38 AM
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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum