marknt Posted September 12, 2006 Share Posted September 12, 2006 Hello people! Im new here. Im on a deadline. I need your help about fread.Here's the scenario:Im reading a textfile with emails on it (one email per line) and i've already parsed it. I already opened the $handle which is $myspace_search_url = "http://search.myspace.com/index.cfm?fuseaction=find.search&searchType=network&interesttype=&country=&searchBy=Email&f_first_name=mark143rlc@yahoo.com&Submit=Find&SearchBoxID=FindAFriend";$handle_url = fopen("$myspace_search_url", "rb");while (!feof($handle_url)) { $contents_url .= fread($handle_url, 8192); }That is already working but in the inputted text file contains for example 3 email addresses. I made a for loop to read all of the emails in the text file. $contents = file($filename);$contents_count = count($contents);for($x=0; $x<$contents_count; $x++){$myspace_search_url = "http://search.myspace.com/index.cfm?fuseaction=find.search&searchType=network&interesttype=&country=&searchBy=Email&f_first_name=".$contents[$x]."&Submit=Find&SearchBoxID=FindAFriend";$handle_url = fopen("$myspace_search_url", "rb");while (!feof($handle_url)) { $contents_url .= fread($handle_url, 8192); }if (strpos($contents_url, "We weren't able to find a") != 0) { // the email has no myspace accountecho "THE EMAIL ADD $contents[$x] HAS NO MYSPACE ACCOUNT!!!<br>";$valid = true;}else if (strpos($contents_url, "We weren't able to find a") == 0) { // the email has a myspace accountecho "THE EMAIL ADD $contents[$x] HAS A MYSPACE ACCOUNT!!!<br>";$valid = false;}fclose($handle_url);} // end of for loopThe problem is if you put it inside a for loop, the $contents_url concatenates so the strpos will be confused in finding the "We weren't able to find a". For example the first email has a myspace acct, the strpos will not find the "We weren't able to find a" string so $valid will return true. In the second loop, it will parse the next email in the text file that is not valid so that it will return false. BUT the problem is the $contents_url now contains two values because of the concatenation. It will find a "We weren't able to find a" AND it will not find "We weren't able to find a" so my validation will not work.Is there any other way for this ---> while (!feof($handle_url)) { $contents_url .= fread($handle_url, 8192); }I mean if in the first loop I will put the contents to a different variable and in the second loop I will put the second content to a different variable? By the way, I have tried this inside the for loop but this is not working ---> while (!feof($handle_url)) { $contents_url .= fread($handle_url, 8192); $contents_url_array[$ctr] = $contents_url; array_push($contents_url_array, $contents_url);}The var_dump($contents_url_array) will output one index(0) with a value of (the concatenated search output which is wrong).......I have 3 loops that will happen in my for loopIs there a way I can put this in my array? ---> array(3) { [0]=> "valid_email" [1]=> "not_valid_email" [2]=> "valid_email" }The text file I have contains 3 email addresses. mark143rlc@yahoo.com <--- this is VALID (Valid = this email has a myspace acct.) not_valid@gmail_yahoo.com <--- this is NOT VALID marknt15@gmail.com <--- this is VALIDThat is all thank you very much! Please help. Thanks in advance. God Bless. Quote Link to comment Share on other sites More sharing options...
btherl Posted September 12, 2006 Share Posted September 12, 2006 Let me guess, you are a C programmer? :)You can fetch a url simply with[code]$contents = file_get_contents('http://www.google.com');[/code]It returns false on failure (that's false, not 0! They are different things in PHP). Then you don't have to worry about concatenating anything.Also, you can loop through an array with[code]foreach ($array as $elem) { .. do something with $elem .. }[/code]No need to count the array and use an iterator.As for strpos(), it returns false if the string is not found. If the string is found, it returns the offset. Again, false is NOT 0, it is false. Return value 0 means the search string was found at the start of the string. So, you should check[code]if (strpos($haystack, $needle) === false) { not found }orif (strpos($haystack, $needle) !== false) { found }[/code] Quote Link to comment Share on other sites More sharing options...
marknt Posted September 12, 2006 Author Share Posted September 12, 2006 Im a PHP Developer. I already solved my own problem *sigh* . As for file_get_contents you said, it is also an alternative option to my code but it is already working.My solution to my problem is resetting the variable $contents_url so that it will not concatenate and il just array_push the separated value to my array. After that I will do a for loop and parse each value in the array. My supervisor from my first work told me to do a regular expression (matching).Thanks btherl! :) 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.