Jump to content

I need to open up a file by loop + array then use preg_match to check the contents how?


et4891

Recommended Posts

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

Link to comment
Share on other sites

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 :D.

 

Denno

Link to comment
Share on other sites

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 :D.

 

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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 by et4891
Link to comment
Share on other sites

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 by et4891
Link to comment
Share on other sites

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 by et4891
Link to comment
Share on other sites

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

Link to comment
Share on other sites

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 by denno020
Link to comment
Share on other sites

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 :P No wonder people love to use trim().....

Edited by et4891
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.