Reading from a file and preg_match()

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Lethality
Forum Newbie
Posts: 16
Joined: Tue May 01, 2007 9:38 am

Post by Lethality »

Alright, I guess I could tell the browser to open all new windows in tabs instead. But how would I proceed to open those links in a new window then?
User avatar
arturm
Forum Commoner
Posts: 86
Joined: Fri Apr 13, 2007 8:29 am
Location: NY
Contact:

Post by arturm »

having this addresses you have to call a javascript function
it can be done something like this:

Code: Select all

foreach ($urls as $url) {
    $js .= "openWindow('".$url."');\n";
}

echo "<script>\n".$js."</script>\n";
but you sill need javascript function called openWindow() that does to job for you to open a new tab (if it is possible in js) or window
Lethality
Forum Newbie
Posts: 16
Joined: Tue May 01, 2007 9:38 am

Post by Lethality »

arturm wrote:having this addresses you have to call a javascript function
it can be done something like this:

Code: Select all

foreach ($urls as $url) {
    $js .= "openWindow('".$url."');\n";
}

echo "<script>\n".$js."</script>\n";
but you sill need javascript function called openWindow() that does to job for you to open a new tab (if it is possible in js) or window
Awesome! :D
It works. The window opening function is called window.open though.
Lethality
Forum Newbie
Posts: 16
Joined: Tue May 01, 2007 9:38 am

Post by Lethality »

I stumbled across a weird problem now as I've filled the inbox with new mail (I deleted the old test mails):
When writing to the file it only writes the last email and not the others. I'm not sure why that happens.

Code: Select all

for ($i = 1; $i <= $count; $i++) {
        $email = $pop3->get_mail($i);

        if ($email == false) {
                echo $pop3->error;
                continue;
        }

        echo '<pre>';
        print_r ($email);
        echo '</pre>';


$file = "email.log";
$fh = fopen($file, 'w') or die("Can't open file");
$readable_email = print_r ($email, true);
fwrite($fh, $readable_email);
fclose($fh);
print_r ($email); still shows me all the emails, but it wont write that same info to the file for some reason.
Last edited by Lethality on Fri May 04, 2007 3:07 pm, edited 1 time in total.
User avatar
arturm
Forum Commoner
Posts: 86
Joined: Fri Apr 13, 2007 8:29 am
Location: NY
Contact:

Post by arturm »

you can do 2 things:
1. open the file before the "for" loop, write to the file in the loop and close the file after the loop (this is the most efficient way)
2. just change fopen function to append the file. fopen() with mode 'a' instead of 'w' will do the job.
Lethality
Forum Newbie
Posts: 16
Joined: Tue May 01, 2007 9:38 am

Post by Lethality »

Ok, that worked but it spawned another annoying problem

Code: Select all

$file = "email.log";
$fh = fopen($file, 'w') or die("Can't open file");

for ($i = 1; $i <= $count; $i++) {
        $email = $pop3->get_mail($i);

        if ($email == false) {
                echo $pop3->error;
                continue;
        }

	$readable_email = print_r($email, true);
	fwrite($fh, $readable_email);
        echo '<pre>';
        print_r ($email);
        echo '</pre>';
}


fclose($fh);

$fh = fopen($file, 'r') or die("Can't open file");
$theData = fread($fh, filesize($file));
fclose($fh);
"Warning: fread() [function.fread]: Length parameter must be greater than 0" ...?
User avatar
arturm
Forum Commoner
Posts: 86
Joined: Fri Apr 13, 2007 8:29 am
Location: NY
Contact:

Post by arturm »

it looks like filesize() returned zero
maybe first show all errors - put this before your code:

Code: Select all

error_reporting(E_ALL);
and then check what's the output:

Code: Select all

echo "size: ".filesize($file)."<br>";
Lethality
Forum Newbie
Posts: 16
Joined: Tue May 01, 2007 9:38 am

Post by Lethality »

This only happens if the inbox is empty, I just replaced the error with a "No emails" message.
I'm trying to make a filter now, to filter out certain urls. Is there an easy way to make a filter before urls are written to the file?
User avatar
arturm
Forum Commoner
Posts: 86
Joined: Fri Apr 13, 2007 8:29 am
Location: NY
Contact:

Post by arturm »

use preg_match() function for filtering
Post Reply