kelphyr Posted February 18, 2007 Share Posted February 18, 2007 Ok, I fetch lines from text files which I need to manipulate but I'm not sure how. I've been asking around but couldn't find a proper answer so that is why I'm asking here. Let's say I've stored a string of text into a variable named $line #1. I need to check if that line has "//" in it or if possible, if it starts with it. I tried a code with the function named strpbrk() but it created errors. Maybe that's not the right way to go at it... How would you do it? #2. How would you about reading something separated by Commas like "this,that,and..." ? I learn from other people's code so an example would very very appreciated. Thank you very much for you cooperation. Quote Link to comment Share on other sites More sharing options...
Orio Posted February 18, 2007 Share Posted February 18, 2007 I didn't understand your second question- but maybe by giving an example you can make things clear? For #1: <?php // $filename holds the file's name $lines = array_map("trim", file($filename)); foreach ($lines as $num => $line) { if(strpos($line, "//") === 0) echo "line #".$num." Starts with a //"; } ?> For question 2 maybe you are looking for the explode() function... Orio. Quote Link to comment Share on other sites More sharing options...
LazyJones Posted February 18, 2007 Share Posted February 18, 2007 1. http://fi2.php.net/manual/en/function.strpos.php strpos($line, "//") will return 0 if line starts with // (some other integer if line contains it) and NULL if it doesn't 2. http://fi.php.net/split with split(",",$line) will return an array of strings (separated by , in array) EDIT: explode will work better for #2 http://fi.php.net/manual/en/function.explode.php thanks, Orio Quote Link to comment Share on other sites More sharing options...
hitman6003 Posted February 18, 2007 Share Posted February 18, 2007 strpos($line, "//") will return 0 if line starts with // (some other integer if line contains it) and NULL if it doesn't strpos will return FALSE if the string isn't found. That is why Orio uses the triple = when determining if the double slash is in his string. Quote Link to comment Share on other sites More sharing options...
kelphyr Posted February 18, 2007 Author Share Posted February 18, 2007 trying to like only echo the lines that dont start with "//" but it doesnt work it just echo bad line.. while (!feof($handle)) { while($line = fgets($handle)){ if(strpos($line, '//')==FALSE){ // Then do nothing echo "<BR>BAD LINE<br>"; }else{ // Process the line echo $line . "<br>"; } } } Quote Link to comment Share on other sites More sharing options...
kelphyr Posted February 19, 2007 Author Share Posted February 19, 2007 help Quote Link to comment Share on other sites More sharing options...
kelphyr Posted February 19, 2007 Author Share Posted February 19, 2007 omg it worked with just a "=" more why 3 "=" ?? $file = $_POST['filename']; $handle = fopen($file, "r"); while (!feof($handle)) { while($line = fgets($handle)){ if(strpos($line,"//")===false){ echo $line . "<br>"; } } } fclose($handle); Quote Link to comment Share on other sites More sharing options...
superuser2 Posted February 19, 2007 Share Posted February 19, 2007 As to explode, here's an example. <?php $list = "item1,item2,item3"; $arr = explode(",", $list) echo $arr[0]; //outputs item1 echo $arr[1]; //outputs item2 echo $arr[2]; //outputs item3 ?> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.