Jump to content

[SOLVED] Capture a line


webstar

Recommended Posts

I'm trying to capture a line in a script. The script itself runs and produces the text as $result

 

I'm trying to capture the underlined line. The issue I am having is it changes depend on the storm it may be longer or shorter  but NEVER changes positions from line 6 so it would be easier to tell it to copy line 6 to $subject.

 

Does this make any sense to anyone what I am trying to do?

 

194

WTNT32 KNHC 290231

TCPAT2

BULLETIN

TROPICAL STORM GUSTAV ADVISORY NUMBER  17

NWS TPC/NATIONAL HURRICANE CENTER MIAMI FL  AL072008

1100 PM EDT THU AUG 28 2008

 

Link to comment
Share on other sites

Use explode() and array indexing.

 

~OR~

 

Use strpos() counting 4 newlines, remember the position.  Use strpos() to capture the 5th newline.  substr() everything in between and trim().

 

 

Small problem... I am php stupid,  no real programming background and what you just said was like german to me.  :'(

Link to comment
Share on other sites

Comes from plain text. And I just underlined it on here to highlight it.

 

A plain text file?

 

//Path tot he file
$file_name = 'directory/file.txt';

//Read fiel into an array
$lines = file($file_name);

//Extract the 6th line of text
$sixth_line = $lines[5];

//Output the data
echo $sixth_line;

 

Although your example appears to have the data on line five - unless there is an empty line at the top.

Link to comment
Share on other sites

That not working and I think it's because the .txt file itself it just one long line rather then split up like the org. text.

 

How hard would it be to just capture the text between "BULLETIN" and "NWS"

 

That will just capture the line I want.

Link to comment
Share on other sites

You should really post the source text in it's original form to get a proper solution. There might be line break characters or other data that may make the solution easier or more difficult. But, yes you can do it by extracting the data between two known values:

 

$text = "194 WTNT32 KNHC 290231 TCPAT2 BULLETIN TROPICAL STORM GUSTAV ADVISORY NUMBER  17 NWS
TPC/NATIONAL HURRICANE CENTER MIAMI FL   AL072008 1100 PM EDT THU AUG 28 2008";

preg_match('/BULLETIN(.*)NWS/', $text, $matches);

$advisory = trim($matches[1]);

echo "Advisory: $advisory";

//Output: "Advisory: TROPICAL STORM GUSTAV ADVISORY NUMBER 17"

Link to comment
Share on other sites

You should really post the source text in it's original form to get a proper solution. There might be line break characters or other data that may make the solution easier or more difficult. But, yes you can do it by extracting the data between two known values:

 

$text = "194 WTNT32 KNHC 290231 TCPAT2 BULLETIN TROPICAL STORM GUSTAV ADVISORY NUMBER  17 NWS
TPC/NATIONAL HURRICANE CENTER MIAMI FL   AL072008 1100 PM EDT THU AUG 28 2008";

preg_match('/BULLETIN(.*)NWS/', $text, $matches);

$advisory = trim($matches[1]);

echo "Advisory: $advisory";

//Output: "Advisory: TROPICAL STORM GUSTAV ADVISORY NUMBER 17"

 

That is a greedy operator, you need to make it non-greedy like so:

 

([^NWS]*) instead of (.*)

Link to comment
Share on other sites

Okay, if we're going to get technical, kratsg, you'd want to use + instead of * because there has to be some text returned apparently.  Oh, and also, the lazy operator is actually ?, but the negative character class is preferred because of the fact that the engine doesn't need to backtrack so much.  xD

Link to comment
Share on other sites

Okay, if we're going to get technical, kratsg, you'd want to use + instead of * because there has to be some text returned apparently.  Oh, and also, the lazy operator is actually ?, but the negative character class is preferred because of the fact that the engine doesn't need to backtrack so much.  xD

 

Fine :-P (dang, I need to go back to my basics of RegEx).. I used to be awesome at it, but my little break from coding made me forget a few simple stuff :-P

 

But yeah. I'm trying to remember somewhere exactly a perfect example that I did for this.. It's way back long time ago o_o And negative character class?

Link to comment
Share on other sites

This might be easier if you tell us how the script is producing the output.. i.e. what is the input?

 

Ken

 

Pretty much I am trying to use the line that is being captured as the subject rather then the current subject.

 

<?
# ----------------------------------------------------------------------------------
# Configuration

# Directory on the remote server.  Be sure to include leading and trailing slashes!
$remotedir = "/text/station/KNHC/";

# File on the remote server.  No slashes!
$remotefile = "WTNT32.KNHC";

# Keyword to check for in response.
$keyword = "$$";

# Remote server address.
$remoteserver = "twister.sbs.ohio-state.edu";

# E-mail recipient(s).  Separate each address with a comma and a space.
$emailrecip = "email address";

# E-mail subject line.
$emailsubject = "[NHC] WTNT32 - Public Advisory";

# E-mail From name.
$emailfromname = "WxServer NHC";

# E-mail From address.
$emailfromaddr = "from address";

$txtfilename = "WTNT32";
# End Configuration
# ----------------------------------------------------------------------------------

# Format the page for easy viewing.
Header( "Content-type: text/plain");

# Setup the request.
$header .= "GET " . $remotedir . $remotefile . " HTTP/1.0\r\n";
$header .= "Host: " . $remoteserver . "\r\n";
$header .= "User-Agent: Downloader\r\n\r\n";
$fp = fsockopen ($remoteserver, 80, $errno, $errstr, 30);

# Do the request.
fputs ($fp, $header . $req) or die("Can't access the site!");

while (!feof($fp)) {
$res .= fgets ($fp, 128);
}

# Strip off the header info.
$headerend = strpos($res,"\r\n\r\n");
if (is_bool($res)) {
$result = $res;
} 
else {
$result = substr($res,$headerend+4,strlen($res) - ($headerend+4));
}

fclose ($fp);

# Check for keyword.
if (!strstr($result, $keyword)) die("Weather Coding not found!");


# Read the file containing the last response.
$filename = $txtfilename . '.txt';
$fp = fopen($filename, "r");
$contents = fread($fp, filesize($filename));
fclose($fp);

# Check for changes.
if ($contents == $result) {
# There has not been a change.
echo ("No Updates\r\n");
} else {
# There has been a change.
echo ("**UPDATES DETECTED**\r\n");

# Write the new file.
$filename = $txtfilename . '.txt';
$fp = fopen($filename, "w");
$write = fputs($fp, $result);
fclose($fp);

# Send the e-mail.
$recipient = $emailrecip;
$subject = $emailsubject;
$body_of_email = $result;

$header = 'From: "' . $emailfromname . '" <' . $emailfromaddr . '>';
$header .= "\n"; 
$header .= "X-Priority: 1";
$header .= "\n";

mail ($recipient, $subject, $body_of_email, $header);

echo ("E-mail sent.");

}
?>

Link to comment
Share on other sites

Okay, if we're going to get technical, kratsg, you'd want to use + instead of * because there has to be some text returned apparently.  Oh, and also, the lazy operator is actually ?, but the negative character class is preferred because of the fact that the engine doesn't need to backtrack so much.  xD

 

Please please don't fight over me, I have enough questions for everyone.  :P

Link to comment
Share on other sites

Okay, if we're going to get technical, kratsg, you'd want to use + instead of * because there has to be some text returned apparently.  Oh, and also, the lazy operator is actually ?, but the negative character class is preferred because of the fact that the engine doesn't need to backtrack so much.  xD

 

Please please don't fight over me, I have enough questions for everyone.   :P

 

*sigh* I chased everyone away.

Link to comment
Share on other sites

Have a look at this:

<?php
# ----------------------------------------------------------------------------------
# Configuration

# Directory on the remote server.  Be sure to include leading and trailing slashes!
$remotedir = "/text/station/KNHC/";

# File on the remote server.  No slashes!
$remotefile = "WTNT32.KNHC";

# Keyword to check for in response.
$keyword = "$$";

# Remote server address.
$remoteserver = "twister.sbs.ohio-state.edu";

# E-mail recipient(s).  Separate each address with a comma and a space.
$emailrecip = "email address";

# E-mail subject line.
$emailsubject = "[NHC] WTNT32 - Public Advisory";

# E-mail From name.
$emailfromname = "WxServer NHC";

# E-mail From address.
$emailfromaddr = "from address";

$txtfilename = "WTNT32";
# End Configuration
# ----------------------------------------------------------------------------------

# Format the page for easy viewing.
Header( "Content-type: text/plain");

# Setup the request.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://" . $remoteserver . $remotedir . $remotefile);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 0);
$res = curl_exec($ch);
curl_close($ch);
$result = array_map('trim',explode("\n",$res));
# Check for keyword.
if (!in_array($keyword,$result)) die("Weather Coding not found!");


# Read the file containing the last response.
$filename = $txtfilename . '.txt';
if (file_exists($filename)) {
$contents = array_map('trim',file($filename));
# Check for changes.
print_r(array_diff(array_unique($contents),array_unique($result)));
if (count(array_diff(array_unique($contents),array_unique($result))) == 0) {
	# There has not been a change.
	echo ("No Updates\r\n");
} else {
	# There has been a change.
	echo ("**UPDATES DETECTED**\r\n");
	# Write the new file.
	echo implode("\n",$result);
	file_put_contents($filename,implode("\n",$result));
}
} else {		
# Write the new file.
echo implode("\n",$result);
file_put_contents($filename,implode("\n",$result));
}
$subject = $result[5];
# Send the e-mail.
$recipient = $emailrecip;
$body_of_email = implode("\n",$result);

$header = 'From: "' . $emailfromname . '" <' . $emailfromaddr . '>';
$header .= "\n"; 
$header .= "X-Priority: 1";
$header .= "\n";

mail ($recipient, $subject, $body_of_email, $header);

echo ("E-mail sent.");
?>

 

I modified your code to use cURL instead of socket reads and a few other modifications.

 

Ken

Link to comment
Share on other sites

Wow, ok I wasn't expecting a rewrite but i'll take it.

 

When  I run the script it does this.

 

<br />

<b>Fatal error</b>:  Call to undefined function curl_init() in <b>C:\WEATHERSCRIPTS\NHCTest\WTNT32.php</b> on line <b>37</b><br />

 

Link to comment
Share on other sites

Ok so the script above works kinda.

 

The output to the screen is 

 

Array

(

)

No Updates

E-mail sent.

 

And it does send the email the only issue is on each refresh it does it again. It isn't writing to the WTNT32.txt file so it has a record of the last email.  So each refresh is a new email again.

Link to comment
Share on other sites

I inadvertently left a debug statment in, so take out the line that says

<?php
print_r(array_diff(array_unique($contents),array_unique($result)));
?>

 

Also, I changed some of the if blocks. If you only want an email when something changes, you would do:

<?php
# ----------------------------------------------------------------------------------
# Configuration

# Directory on the remote server.  Be sure to include leading and trailing slashes!
$remotedir = "/text/station/KNHC/";

# File on the remote server.  No slashes!
$remotefile = "WTNT32.KNHC";

# Keyword to check for in response.
$keyword = "$$";

# Remote server address.
$remoteserver = "twister.sbs.ohio-state.edu";

# E-mail recipient(s).  Separate each address with a comma and a space.
$emailrecip = "email address";

# E-mail subject line.
$emailsubject = "[NHC] WTNT32 - Public Advisory";

# E-mail From name.
$emailfromname = "WxServer NHC";

# E-mail From address.
$emailfromaddr = "from address";

$txtfilename = "WTNT32";
# End Configuration
# ----------------------------------------------------------------------------------

# Format the page for easy viewing.
Header( "Content-type: text/plain");

# Setup the request.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://" . $remoteserver . $remotedir . $remotefile);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 0);
$res = curl_exec($ch);
curl_close($ch);
$result = array_map('trim',explode("\n",$res));
# Check for keyword.
if (!in_array($keyword,$result)) die("Weather Coding not found!");


# Read the file containing the last response.
$filename = $txtfilename . '.txt';
if (file_exists($filename)) {
$contents = array_map('trim',file($filename));
# Check for changes.
if (count(array_diff(array_unique($contents),array_unique($result))) == 0) {
	# There has not been a change.
	echo ("No Updates\r\n");
} else {
	# There has been a change.
	echo ("**UPDATES DETECTED**\r\n");
	# Write the new file.
	echo implode("\n",$result);
	file_put_contents($filename,implode("\n",$result));
	        $subject = $result[5];
# Send the e-mail.
        $recipient = $emailrecip;
        $body_of_email = implode("\n",$result);

        $header = 'From: "' . $emailfromname . '" <' . $emailfromaddr . '>';
        $header .= "\n"; 
        $header .= "X-Priority: 1";
        $header .= "\n";

	        mail ($recipient, $subject, $body_of_email, $header);

        echo ("E-mail sent.");
}
} else {		
# Write the new file.
echo implode("\n",$result);
file_put_contents($filename,implode("\n",$result));
}
?>

 

Ken

Link to comment
Share on other sites

I inadvertently left a debug statment in, so take out the line that says

<?php
print_r(array_diff(array_unique($contents),array_unique($result)));
?>

 

Also, I changed some of the if blocks. If you only want an email when something changes, you would do:

<?php
# ----------------------------------------------------------------------------------
# Configuration

# Directory on the remote server.  Be sure to include leading and trailing slashes!
$remotedir = "/text/station/KNHC/";

# File on the remote server.  No slashes!
$remotefile = "WTNT32.KNHC";

# Keyword to check for in response.
$keyword = "$$";

# Remote server address.
$remoteserver = "twister.sbs.ohio-state.edu";

# E-mail recipient(s).  Separate each address with a comma and a space.
$emailrecip = "email address";

# E-mail subject line.
$emailsubject = "[NHC] WTNT32 - Public Advisory";

# E-mail From name.
$emailfromname = "WxServer NHC";

# E-mail From address.
$emailfromaddr = "from address";

$txtfilename = "WTNT32";
# End Configuration
# ----------------------------------------------------------------------------------

# Format the page for easy viewing.
Header( "Content-type: text/plain");

# Setup the request.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://" . $remoteserver . $remotedir . $remotefile);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 0);
$res = curl_exec($ch);
curl_close($ch);
$result = array_map('trim',explode("\n",$res));
# Check for keyword.
if (!in_array($keyword,$result)) die("Weather Coding not found!");


# Read the file containing the last response.
$filename = $txtfilename . '.txt';
if (file_exists($filename)) {
$contents = array_map('trim',file($filename));
# Check for changes.
if (count(array_diff(array_unique($contents),array_unique($result))) == 0) {
	# There has not been a change.
	echo ("No Updates\r\n");
} else {
	# There has been a change.
	echo ("**UPDATES DETECTED**\r\n");
	# Write the new file.
	echo implode("\n",$result);
	file_put_contents($filename,implode("\n",$result));
	        $subject = $result[5];
# Send the e-mail.
        $recipient = $emailrecip;
        $body_of_email = implode("\n",$result);

        $header = 'From: "' . $emailfromname . '" <' . $emailfromaddr . '>';
        $header .= "\n"; 
        $header .= "X-Priority: 1";
        $header .= "\n";

	        mail ($recipient, $subject, $body_of_email, $header);

        echo ("E-mail sent.");
}
} else {		
# Write the new file.
echo implode("\n",$result);
file_put_contents($filename,implode("\n",$result));
}
?>

 

Ken

 

Thank you for your help. It works good.

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.