et4891 Posted February 23, 2013 Share Posted February 23, 2013 ok I'm suppose to call up a certain txt file then using loop then check the contents let's say if the first eight words are set how it should be and if it is an actual email address blah blah.... I don't have much because I'm totally stuck but this is what I have for now.... $fileContents = file("./aaa/aaa.txt); foreach($fileContents as $row) { } totally don't have much.....because I'm totally confused.... and let's say in that aaa.txt this is what's inside a456789,baloney,tony,t_baloney@shaw.ca a221111,lasty,firsty,e@mail.ca but I have to then check if the first part is start with a and exact 6 numbers after no more or less and also check if the email is valid...like email contains xxxxx@xxx.xx anyone able to give me a clue, a hand or help me out please? Thanks in advance.... Quote Link to comment https://forums.phpfreaks.com/topic/274846-i-need-to-open-up-a-file-by-loop-array-then-use-preg_match-to-check-the-contents-how/ Share on other sites More sharing options...
trq Posted February 23, 2013 Share Posted February 23, 2013 I'm not sure we can help without blah blah blah.... Quote Link to comment https://forums.phpfreaks.com/topic/274846-i-need-to-open-up-a-file-by-loop-array-then-use-preg_match-to-check-the-contents-how/#findComment-1414349 Share on other sites More sharing options...
denno020 Posted February 23, 2013 Share Posted February 23, 2013 Inside your foreach statement you want an if statement as such: if(!preg_match("your pattern here", $row)){ //the preg match wasn't satisfied, so there is an error } The exclamation point at the beggining means not, so therefore, if the preg match fails, then the code inside the if will run, which is where you should report back at error to the user. The tricky thing will be to work out what the regular expression is that you need. If you can come up with something, I'll be happy to guide you from there, but I'm not going to let you off the hook and write it all for you . Denno Quote Link to comment https://forums.phpfreaks.com/topic/274846-i-need-to-open-up-a-file-by-loop-array-then-use-preg_match-to-check-the-contents-how/#findComment-1414357 Share on other sites More sharing options...
et4891 Posted February 23, 2013 Author Share Posted February 23, 2013 Inside your foreach statement you want an if statement as such: if(!preg_match("your pattern here", $row)){ //the preg match wasn't satisfied, so there is an error } The exclamation point at the beggining means not, so therefore, if the preg match fails, then the code inside the if will run, which is where you should report back at error to the user. The tricky thing will be to work out what the regular expression is that you need. If you can come up with something, I'll be happy to guide you from there, but I'm not going to let you off the hook and write it all for you . Denno Hi denno...actually I figured out the first part...but somehow the preg_match part for the email....I just keep on messing up at the last part....I wonder if I'm too tired at reading all the letters or what....I just kept on getting errors.... I have this... if((preg_match("/^a[0-9]{6}$/i", $column[0])) && (preg_match("/^([0-9]|[a-z])([0-9]|[a-z]|[_-])*@([0-9]|[a-z])*\.([0-9]|[a-z]){2,4}$/i", $column[3]))) first preg_match is to match the eh ya a###### but the email part I am told to check at least this... alphanumeric characters followed by @ followed by alphanumeric characters followed by . followed by 2 – 4 more alphanumeric characters and the above is what I came up with but I keep on getting errors....like if my list has 10 emails...the last one will be correct and the others will be error....if my list has 5...still the last one is correct and the other 4 are errors.... Quote Link to comment https://forums.phpfreaks.com/topic/274846-i-need-to-open-up-a-file-by-loop-array-then-use-preg_match-to-check-the-contents-how/#findComment-1414362 Share on other sites More sharing options...
denno020 Posted February 23, 2013 Share Posted February 23, 2013 Where has $column come from? What I would do it just do a var_dump or echo of $column to make sure you are actually checking the right portion of the line of text. Also, it may just be the way you've typed it here, but if there is a space before the first (or any) line of your aaa.txt contents, then that probably won't help things.. Quote Link to comment https://forums.phpfreaks.com/topic/274846-i-need-to-open-up-a-file-by-loop-array-then-use-preg_match-to-check-the-contents-how/#findComment-1414363 Share on other sites More sharing options...
et4891 Posted February 23, 2013 Author Share Posted February 23, 2013 (edited) Where has $column come from? What I would do it just do a var_dump or echo of $column to make sure you are actually checking the right portion of the line of text. Also, it may just be the way you've typed it here, but if there is a space before the first (or any) line of your aaa.txt contents, then that probably won't help things.. oh the $column is from.. $fileContents = file("./aaa/. $_GET["filename"]); foreach($fileContents as $row) { $column = preg_split("/,/", $row); if I only echo column...this error will show... Notice: Array to string conversion in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\test\eee.php on line 23 Array but if I use echo column[#] it does show the text I want though Edited February 23, 2013 by et4891 Quote Link to comment https://forums.phpfreaks.com/topic/274846-i-need-to-open-up-a-file-by-loop-array-then-use-preg_match-to-check-the-contents-how/#findComment-1414366 Share on other sites More sharing options...
denno020 Posted February 23, 2013 Share Posted February 23, 2013 use var_dump($column); That will print the array. Quote Link to comment https://forums.phpfreaks.com/topic/274846-i-need-to-open-up-a-file-by-loop-array-then-use-preg_match-to-check-the-contents-how/#findComment-1414367 Share on other sites More sharing options...
et4891 Posted February 23, 2013 Author Share Posted February 23, 2013 (edited) use var_dump($column); That will print the array. prints out something like this... array(4) { [0]=> string(9) "a00456789" [1]=> string(7) "baloney" [2]=> string(4) "tony" [3]=> string(19) "t_baloney@shaw.ca " } array(4) { [0]=> string(9) "a00111111" [1]=> string(5) "lasty" [2]=> string(6) "firsty" [3]=> string(9) "e@mail.ca" } really sorry about this...totally new with php.... so the email part is on array 3....but why is my validation so messed up >.<" it seems fine if I take out the last part after \. but what I added after \. things start to go wrong...because each step I'll refresh and check few times the step I did is right so if I troubleshoot I don't need to go all the way back...but somehow this just.....>.<" Edited February 23, 2013 by et4891 Quote Link to comment https://forums.phpfreaks.com/topic/274846-i-need-to-open-up-a-file-by-loop-array-then-use-preg_match-to-check-the-contents-how/#findComment-1414368 Share on other sites More sharing options...
denno020 Posted February 23, 2013 Share Posted February 23, 2013 Well from that I see that $column[3] has a space at the end.. This will mess up your matching. Perhaps if you run trim() on $column[3] before you run the preg match, this might help.. That way any leading or trailing spaces will be removed. Quote Link to comment https://forums.phpfreaks.com/topic/274846-i-need-to-open-up-a-file-by-loop-array-then-use-preg_match-to-check-the-contents-how/#findComment-1414370 Share on other sites More sharing options...
et4891 Posted February 23, 2013 Author Share Posted February 23, 2013 (edited) Well from that I see that $column[3] has a space at the end.. This will mess up your matching. Perhaps if you run trim() on $column[3] before you run the preg match, this might help.. That way any leading or trailing spaces will be removed. still the same >.<" I added trim($column[3]); before the if(preg_match because if I do trim($column); error happens wait wait >.<" omg....I guess thinking too much giving me more headaches and errors...one sec...let me try one more thing I missed something in trim Edited February 23, 2013 by et4891 Quote Link to comment https://forums.phpfreaks.com/topic/274846-i-need-to-open-up-a-file-by-loop-array-then-use-preg_match-to-check-the-contents-how/#findComment-1414371 Share on other sites More sharing options...
et4891 Posted February 23, 2013 Author Share Posted February 23, 2013 oops...I can't edit the previous post anymore....OMG!!!!! I think it worked....OMG!!! thanks....so var_dump is pretty useful that way....because I was like more than 100% sure in my .txt file my text...it's totally NO space....why does it suddenly create a space after it's being called out? I didn't double triple 4x check.....often I would go back to the text file change thing and see if I'm doing it correctly...but it's 2 am and I've been thinking for the past hours I will do the double triple check tomorrow....OMG Denno can I give you a hug? hahaha..... P.S. sorry can I ask something that's out of the php field? how do I delete my own post or like the previous reply/quote I made....how do I delete it if I want to? Or there's no way to delete it....I'm still kind of new in this forum place.... Quote Link to comment https://forums.phpfreaks.com/topic/274846-i-need-to-open-up-a-file-by-loop-array-then-use-preg_match-to-check-the-contents-how/#findComment-1414376 Share on other sites More sharing options...
denno020 Posted February 23, 2013 Share Posted February 23, 2013 Put the trim inside the preg_match as such: if((preg_match("/^a[0-9]{6}$/i", trim($column[0]))) && (preg_match("/^([0-9]|[a-z])([0-9]|[a-z]|[_-])*@([0-9]|[a-z])*\.([0-9]|[a-z]){2,4}$/i", trim($column[3])))) Quote Link to comment https://forums.phpfreaks.com/topic/274846-i-need-to-open-up-a-file-by-loop-array-then-use-preg_match-to-check-the-contents-how/#findComment-1414377 Share on other sites More sharing options...
et4891 Posted February 23, 2013 Author Share Posted February 23, 2013 denno again...thanks! you saved my life! tomorrow I gotta carry on with the works...hopefully I won't be stuck again... Quote Link to comment https://forums.phpfreaks.com/topic/274846-i-need-to-open-up-a-file-by-loop-array-then-use-preg_match-to-check-the-contents-how/#findComment-1414378 Share on other sites More sharing options...
denno020 Posted February 23, 2013 Share Posted February 23, 2013 (edited) No worries mate, glad to have helped. I think that maybe the space is added in place of the 'new line character' that you won't see in a text editor, but php will pick it up.. That's my theory anyways . Oh and there's not need to delete your post, not that you can, but it's worth staying there so other people can follow your thought process and it can help them, should they need it. Edited February 23, 2013 by denno020 Quote Link to comment https://forums.phpfreaks.com/topic/274846-i-need-to-open-up-a-file-by-loop-array-then-use-preg_match-to-check-the-contents-how/#findComment-1414379 Share on other sites More sharing options...
et4891 Posted February 23, 2013 Author Share Posted February 23, 2013 (edited) oh hehe....oh it's just somethings like this...that I'm frustrated....there was like once I missed a ; or something and I posted somewhere else not here. I posted for help...then few minutes later I went for a quick food...which only took few minutes...then come back...and I saw the stupid mistake....and that's the time I want to delete my post haha.... and somehow the spaces aren't added to the last array... so weird >.<" but I guess that's a lesson for me No wonder people love to use trim()..... Edited February 23, 2013 by et4891 Quote Link to comment https://forums.phpfreaks.com/topic/274846-i-need-to-open-up-a-file-by-loop-array-then-use-preg_match-to-check-the-contents-how/#findComment-1414380 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.