Jump to content


Please help (fread), im on a deadline thank you

  • Please log in to reply
2 replies to this topic

#1 marknt

  • Members
  • PipPipPip
  • Advanced Member
  • 34 posts
  • LocationPhilippines

Posted 12 September 2006 - 03:24 AM

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.myspac...D=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.myspac...ame=".$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 account
echo "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 account
echo "THE EMAIL ADD $contents[$x] HAS A MYSPACE ACCOUNT!!!<br>";
$valid = false;

} // end of for loop

The 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 loop

Is 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 VALID

That is all thank you very much! Please help. Thanks in advance. God Bless.

#2 btherl

  • Staff Alumni
  • Advanced Member
  • 3,893 posts
  • LocationAustralia

Posted 12 September 2006 - 07:03 AM

Let me guess, you are a C programmer? :)

You can fetch a url simply with

$contents = file_get_contents('http://www.google.com');

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

foreach ($array as $elem) {  .. do something with $elem .. }

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

if (strpos($haystack, $needle) === false) { not found }
if (strpos($haystack, $needle) !== false) { found }

#3 marknt

  • Members
  • PipPipPip
  • Advanced Member
  • 34 posts
  • LocationPhilippines

Posted 12 September 2006 - 07:17 AM

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!  :)

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users