5 Replies - 1975 Views - Last Post: 18 June 2010 - 01:32 PM Rate Topic: -----

#1 cpuguru07  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 1
  • View blog
  • Posts: 33
  • Joined: 21-February 09

if $string does not contain numbers

Posted 18 June 2010 - 12:17 PM

I have a text file containing contact information for multiple people. The contact information is stored as follows (formatting is inconsistent - some lines have quotations marks and some don't):
1:		John Smith	
			
		Company
		"123 Some Street"	
		"Somewhere, PA, 12345"	
		(123)456-7890	
		john.smith@company.com	
			
		"For more information..."	
		"For driving directions..."	



I am trying to parse this information into a Tab separated Text file by reading it in one line at a time and determining what type of data it is, then storing it until the next record is reached, then formatting it as a tab separated line and writing it to the output file.

My problem is that the company name is not being written to the file. I know the company name is being retrieved because it can be echoed out.

(The way I am filtering, I assume the company name does not contain numbers, where as the name and address fields do and the other lines are filtered based upon the "@" symbol for the email address and the word "For " being at the beginning of the other lines.)

My code is as follows:
$i = 0;

/* formats data and writes to output file - first pass is for column headings, which the parameters are initialized to at beginning of script */
function end_of_line($output, $contact = "", $company = "", $street = array(), $city = "", $state = "", $zip = "", $phone = "", $email = "", $website = "")
{
	$street_print = $street[0] . "\"\t\"" . $street[1] . "\"\t\"" . $street[2];
	
	$output_line = "\"" . $contact . "\"\t\"" . $company . "\"\t\"";
	$output_line .= $street_print . "\"\t\"" . $city . "\"\t\"" . $state . "\"\t\"" . $zip . "\"\t\"";
	$output_line .= $phone . "\"\t\"" . $email . "\"\t\"" . $website . "\"\r\n";
	
	fwrite($output, $output_line);
}

$dir = "\\\\network_path\\";

$input_file_name = "input.txt";
$input_file = $dir . $input_file_name;

$output_file_name = "output.txt";
$output_file = $dir . $output_file_name;

$output = fopen($output_file, "w");
$input = fopen($input_file, "r");

while(!feof($input))
{
	$input_line = trim(fgets($input));
	
	/* get rid of quotation marks around lines if there are any */
	if(substr($input_line, 0, 1) == "\"")
		$input_line = substr($input_line, 1);
	if(substr($input_line, strlen($input_line)-1, 1) == "\"")
		$input_line = substr($input_line, 0, strlen($input_line)-1);
	
	/* if the line has a colon in it, then it is the first line in a new record and contains the contact name */
	if(!strpos($input_line, ":") === FALSE)
	{
		end_of_line($output, $contact, $company, $street, $city, $state, $zip, $phone, $email);
		// reset variables for new contact
		$i = 0;
		$contact = "";
		$company = "";
		$street = array();
		$city = "";
		$state = "";
		$zip = "";
		$phone = "";
		$email = "";
		
		// get contact name
		$contact = trim(substr($input_line, strpos($input_line, ":") + 3));
		if(substr($contact, 0, 1) == "\"") //remove additional quotation marks
			$contact = substr($contact, 1);
		//echo $contact;
	}
	else if(preg_match("[@]",$input_line)) /* detects and stores email address */
	{
		$email = $input_line;
	}
	else if(strlen(preg_replace('/[^0-9]/', "", $input_line)) >= 10) /* detects and stores phone number */
	{
		$phone = $input_line;
	}
	/******SHOULD RETRIEVE COMPANY NAME, BUT DOESN'T******/
	else if(preg_match('/[0-9]/', $input_line) == 0 && substr($input_line, 0, 4) != "For ") 
	{
		$company = $input_line;
		echo $company . "\n";
	}
	else if($input_line != "" && substr($input_line, 0, 4) != "For ")
	{
		if(!strpos($input_line, ",") === FALSE && !is_numeric(substr($input_line, 0, 1)))
		{
			$city = substr($input_line, 0, strpos($input_line, ","));
			$state = substr($input_line, strpos($input_line, ",")+2, 2);
			$zip = substr($input_line, strrpos($input_line, " ")+1);
		}
		else
		{
			$street[$i] = $input_line;
			$i++;
			//echo $street;
		}
	}
}

end_of_line($output, $contact, $company, $street, $city, $state, $zip, $phone, $email);

fclose($input);
fclose($output);



Can anyone tell me what I'm doing wrong?

Is This A Good Question/Topic? 0
  • +

Replies To: if $string does not contain numbers

#2 KaffineAddict  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 24
  • Joined: 23-October 09

Re: if $string does not contain numbers

Posted 18 June 2010 - 12:55 PM

So for clarification
echo $company . "\n";

will echo the company name if this where to run, but it is not being written to the file correct?
Was This Post Helpful? 0
  • +
  • -

#3 cpuguru07  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 1
  • View blog
  • Posts: 33
  • Joined: 21-February 09

Re: if $string does not contain numbers

Posted 18 June 2010 - 12:59 PM

Yes, that is correct. It's been baffling me for the better half of the day and I just can't seem to figure out why it's not writing it to the file!
Was This Post Helpful? 0
  • +
  • -

#4 KaffineAddict  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 24
  • Joined: 23-October 09

Re: if $string does not contain numbers

Posted 18 June 2010 - 01:22 PM

FINALLY! I found it for you man... the check for a company name needs to be like this:
else if(preg_match('/[0-9]/', $input_line) == 0 && substr($input_line, 0, 4) != "For " && $input_line != "") 


On empty lines it was setting company to ""

it took me like ten echo statements all over the place to get it... I hate stupid little errors like this they are so frustrating :P

This post has been edited by KaffineAddict: 18 June 2010 - 01:23 PM

Was This Post Helpful? 0
  • +
  • -

#5 cpuguru07  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 1
  • View blog
  • Posts: 33
  • Joined: 21-February 09

Re: if $string does not contain numbers

Posted 18 June 2010 - 01:29 PM

Thank you SO MUCH! Now that I see the solution, it is obvious why it wasn't working!

I know what you mean! The last one was that I forgot a semicolon. I feel like such a n00b when I do things like that!

Now I just need to figure out why it's not pulling all of the addresses quite right...probably something similar. Maybe letting it sit over the weekend will help!
Was This Post Helpful? 0
  • +
  • -

#6 KaffineAddict  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 24
  • Joined: 23-October 09

Re: if $string does not contain numbers

Posted 18 June 2010 - 01:32 PM

LOL no... we all do that... I always am caught using > instead of <

the problem usually is the fact that I can stare at my own code for twenty minutes knowing that it can't be my fault so I never see it, and then someone else fixes it and I feel pretty stupid, but we all miss the little things so it always helps to have someone else look through it.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1