HAMM3R Posted October 27, 2006 Share Posted October 27, 2006 HeyIve got a text file. It has many lines of data.I need a script that will loop through and if the line matches this:String1.level=Int1(and example would be: HAMM3R.level=4)If Int1 >= 3, i need it to echo something like String1 = Int1So, if a line is HAMM3R.level=4 then it will echo "HAMM3R = 4"If its HAMM3R.level=2 it will go to the next lineAnd if the line doesnt match the String.level=Int format that it will also go to the next lineI know im not giving you anything to start with but im new at this. I hope someone can help me out with this.Thanks in advance!HAMM3R Link to comment https://forums.phpfreaks.com/topic/25263-loop-through-txt-file-return-value/ Share on other sites More sharing options...
bqallover Posted October 27, 2006 Share Posted October 27, 2006 You need to use regular expressions for this kind of thing. The following is untested but *should* work...[code]<?php $lines = file('data.txt'); // or whatever it's called foreach( $lines as $line ) { if( eregi( "([a-z0-9]+)\.level=([0-9]+)", $line, $regs ) ) { if( $regs[2] >= 3 ) { echo "$regs[1] = $regs[2]\n"; } } }?>[/code] Link to comment https://forums.phpfreaks.com/topic/25263-loop-through-txt-file-return-value/#findComment-115207 Share on other sites More sharing options...
HAMM3R Posted October 27, 2006 Author Share Posted October 27, 2006 Great! That works just as I needed.Thanks bqallover ;D Link to comment https://forums.phpfreaks.com/topic/25263-loop-through-txt-file-return-value/#findComment-115218 Share on other sites More sharing options...
HAMM3R Posted October 27, 2006 Author Share Posted October 27, 2006 Hey one more question. Some of the users are in clans so have clan tags in their names. Right now it wont display anything other than letters and number (as the regex allows). Could you please show me how to make it accept [] = - | () etc? Thanks! Link to comment https://forums.phpfreaks.com/topic/25263-loop-through-txt-file-return-value/#findComment-115574 Share on other sites More sharing options...
Caesar Posted October 27, 2006 Share Posted October 27, 2006 Try this...[code]<?php $lines = file('data.txt'); // or whatever it's called foreach( $lines as $line ) { if(eregi( "(.*)\.level=([0-9]+)", $line, $regs); ) { if( $regs[2] >= 3 ) { echo "$regs[1] = $regs[2]\n"; } } }?>[/code] Link to comment https://forums.phpfreaks.com/topic/25263-loop-through-txt-file-return-value/#findComment-115580 Share on other sites More sharing options...
HAMM3R Posted October 27, 2006 Author Share Posted October 27, 2006 Thanks Caesar Link to comment https://forums.phpfreaks.com/topic/25263-loop-through-txt-file-return-value/#findComment-115607 Share on other sites More sharing options...
Caesar Posted October 27, 2006 Share Posted October 27, 2006 You're welcome. My pleasure. Link to comment https://forums.phpfreaks.com/topic/25263-loop-through-txt-file-return-value/#findComment-115609 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.