phpllama Posted December 24, 2007 Share Posted December 24, 2007 I was wonderin how to read a line and was wonderin if you guys could help me out here. Say the line was something like this $line = "abcdefg hijlkmnop ltwao 348333" Now I want to search $line for hijlkmnop, if it is there then search for the next integer which would be "348333" I want to search for the next integer not the number 348333, because 348333 will change constantly. Appreciate anyones help. Quote Link to comment https://forums.phpfreaks.com/topic/83070-solved-readline/ Share on other sites More sharing options...
corbin Posted December 24, 2007 Share Posted December 24, 2007 It depends on how much the data changes. If it always stays with: <letters> <letters> <letters> <number> You could simply explode() the space assuming the space stays the same, and none of the data would ever include a space. You could also use regular expressions, but I don't think they would offer an advantage here, unless you want to pull just the number, regardless of how the rest of the string was formatted. http://php.net/explode $line = "abcdefg hijlkmnop ltwao 348333"; $e = explode(' ', $line); echo $line[3]; //348333 Quote Link to comment https://forums.phpfreaks.com/topic/83070-solved-readline/#findComment-422578 Share on other sites More sharing options...
phpllama Posted December 24, 2007 Author Share Posted December 24, 2007 Thank you that will be a great help I just need help with another thing, reading only one line in a file lines.html <html> <head> <title>lines</title> <head> <body> a line another line lalala another </body> <html> Now I want it to read ONLY line 8 (lalala) and load it into $keep Quote Link to comment https://forums.phpfreaks.com/topic/83070-solved-readline/#findComment-422580 Share on other sites More sharing options...
corbin Posted December 24, 2007 Share Posted December 24, 2007 $keep = file('file_name.ext'); $keep = $keep[7]; I would advise against file though if the file is large (like very large... in the MBs) since it loads the entire file at the same time into memory. Quote Link to comment https://forums.phpfreaks.com/topic/83070-solved-readline/#findComment-422584 Share on other sites More sharing options...
phpllama Posted December 24, 2007 Author Share Posted December 24, 2007 Thanks again, I tried it but what if the file is not on the same server like i want to link it out to a url http://samplesite.com/lines.html <html> <head> <title>lines</title> <head> <body> a line another line lalala another </body> <html> $keep = file('http://samplesite.com/lines.html'); $keep = $keep[7]; does not work Quote Link to comment https://forums.phpfreaks.com/topic/83070-solved-readline/#findComment-422589 Share on other sites More sharing options...
phpllama Posted December 24, 2007 Author Share Posted December 24, 2007 anyone know how to do this? ??? im really stuck and I need to get this done Quote Link to comment https://forums.phpfreaks.com/topic/83070-solved-readline/#findComment-422606 Share on other sites More sharing options...
raku Posted December 24, 2007 Share Posted December 24, 2007 Try using file_get_contents instead of file Quote Link to comment https://forums.phpfreaks.com/topic/83070-solved-readline/#findComment-422619 Share on other sites More sharing options...
phpllama Posted December 24, 2007 Author Share Posted December 24, 2007 it only returns one letter Quote Link to comment https://forums.phpfreaks.com/topic/83070-solved-readline/#findComment-422623 Share on other sites More sharing options...
phpllama Posted December 24, 2007 Author Share Posted December 24, 2007 This is what I tried readit.php <?php $keep = file_get_contents('http://localhost/file.html'); echo $keep[20]; ?> file.html <html> <head> <title>lines</title> <head> <body> a line another line lalala another </body> <html> and it returned the letter l Quote Link to comment https://forums.phpfreaks.com/topic/83070-solved-readline/#findComment-422624 Share on other sites More sharing options...
corbin Posted December 24, 2007 Share Posted December 24, 2007 That's because file_get_contents returns the entire thing, not an array of lines.... You would want to do: $keep = file_get_contents('http://localhost/file.html'); $keep = explode("\n", $keep); $keep = $keep[20]; //depending on the line breaks, \n might need to be \r or \r\n. When you access a string like an array, it returns that value. Example: $str = "Corbin"; echo $str[0]; //C echo $str[3]; //b Quote Link to comment https://forums.phpfreaks.com/topic/83070-solved-readline/#findComment-422638 Share on other sites More sharing options...
phpllama Posted December 25, 2007 Author Share Posted December 25, 2007 thanks for the help Quote Link to comment https://forums.phpfreaks.com/topic/83070-solved-readline/#findComment-422691 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.