It appears that you might have got into a mess with your regular exprssions. The reason you appear to be using them here is to check for the presence of data.
Let us evaluate the one you have in your first if statement:
if (ereg ("([^0-9])(,)", $string, $regs))
This regular expression "([^0-9])(,)" will match a single non numerical character followed by a comma "," anywhere in $string. Lets use your second address and apply this expression:
Ziemb
a, & Darlene
275-2949
Teen 345-2332
2720 Overlook Cr.
(2078)
The text in bold is where the match would have occured. You then load the entire contents of $string into the array item [name].
Now the second if statement:
if (ereg ("([1-9]\-[1-9])", $string) && !(ereg ("(Teen)", $string)))
This regular expression "([1-9]\-[1-9])" will match a number between 1 and 9 not including 0 a "-" and another number between 1 and 9. Lets now apply this regular expression to the variable $string:
Ziemba, & Darlene
275-2949
Teen 34
5-2332
2720 Overlook Cr.
(2078)
This regular expression matches the string hopwever you only proceed if the string contains the word "Teen" to load the entire contents of $string into the array item [phone]
The third if statement:
if (ereg ("(Teen)", $string))
Ziemba, & Darlene
275-2949
Teen 345-2332
2720 Overlook Cr.
(2078)
Quite simply you proceed to load the entire contents of $string variable into the array item [tphone].
I'm quite sure that this behaviour is not what you intended. The main thing wrong with the regular expressions is that they do not recognise the repitition of characters. e.g. to match your telephone number [1-9]\-[1-9] matches only three characters and therefore, would not match 123-1234. It appears that you are also making the assumption that the ereg function somehow modifies the $string variable.
Assuming that the data you supply is in the following format you can contstruct serveral regular expressions to match each data item and then join them together in one regular expression to match the entire string.
I am assuming the following format:
Code:
[name]{NL}
[number]{NL}
Teen [teen]{NL}
[address]{NL}
[lot]{NL}
{NL} = new line character
[teen] and [number] shall be in the format xxx-xxxx
[lot] shall be in the format (:number:)
Firstly to match name we can use the following regular expression:
(.+)\n
The period "." will match an character except newline characters.
The plus "+" requires that there be one or more of the preceeding character.
and "\n" matches a newline character.
Anything included in brackets i.e "(.+)" will be captured separatly. Therefore this regular expression will match:
"Ziemba, & Darlene{nl}" and capture the text "Ziemba, & Darlene"
We then need to match the first telephone number:
([0-9]{3}\-[0-9]{4})\n
The character class [0-9] will match any numeric character.
{3} means there must be exactly 3 of the preceeding character.
\- matches the - character
[0-9]{4} matches exatcly 4 numeric characters.
Finally we match the newline characrer.
Therefore this regular expression will match:
"275-2949{NL}" and capture the text "275-2949"
We then need to match the teen telephone number:
(Teen ([0-9]{3}\-[0-9]{4})\n)?
This regular is similar to the one that matches the number however it is enclosed entirely in brackets. This is refrrered to as grouping. As the teen number may or may not appear we use the "?" to signify that there will be either zero or one of the preceeding grouped expression.
Therefore this regualr expression will match:
"Teen 345-2332{NL}" and capture the text "Teen 345-2332{NL}" and "345-2332"
Next to match the address:
(.+)\n
This will match a single line containing one or more characters and will therefore, match:
"2720 Overlook Cr.{NL}" and capture the text "2720 Overlook Cr."
Finally to match the lot number:
\(([0-9]+)\)
Note to match the barkets they have to be excaped. This expression will match one or more numberical characters enclosed in brakets:
"(2078)" will be matches and the text "2078" will be captured.
Finally we need to put the regular expression together. When we do we come up with this:
(.+)\n([0-9]{3}\-[0-9]{4})\n(Teen ([0-9]{3}\-[0-9]{4})\n)?(.+)\n\(([0-9]+)\)
This will match and entire string containing all the details in the format I mentioned above. If it does not then we can assume the data is corrupt.
The following script uses this regular expression and also the preg_match() function. I prefer the perl style regular expressions as they are a lot more powerful. The function is as follows:
preg_match (regex, string, matches);
The regex argument should be a valid regular expression enclosed in solidus' i.e "/regex/". The string is the text the regular expression is being applied to.
The matches argument is used to load the match and all the captured text into an array. i.e. if this expression were applied to the string I used above $matches would be an array in the following format:
Code:
$matches[0] =>
"Ziemba, & Darlene
275-2949
Teen 345-2332
2720 Overlook Cr.
(2078)"
$matches[1] => "Ziemba, & Darlene"
$matches[2] => "275-2949"
$matches[3] => "Teen 345-2332"
$matches[4] => "345-2332"
$matches[5] => "2720 Overlook Cr."
$matches[6] => "(2078)"
I hope this script is what you a looking for. If not then hopfully you can modify the regular expression to fit your needs:
PHP Code:
<?php
/* load in some test data */
$rawdata = Array (
"Zeller, Charles & Eve
453-5432
2509 Jefferson Road
(265)",
"Ziemba, & Darlene
275-2949
Teen 345-2332
2720 Overlook Cr.
(2078)",
"Zimmer, Richard
255-2600
1528 Trumball Terrace
(4333)"
);
for($i=0; $i < 3; $i++) { /* N.B: don't enclose numbers in quotes unless they are text - it confuses things */
$string = $rawdata[$i];
/* this regular expression matches an entire $string in the following format:
*
* name[NL]
* number[NL] (number must be in format xxx-xxxx)
* Teen teen[NL] (optional - if present must be in format xxx-xxxx)
* address[NL]
* lot[NL] (must be in format (:number:))
*
* where [NL] = newline character
*
* If the data in $string fails to conform to this format it will be skipped.
* The data collected from this expression is loaded into the array $matches by the preg_match() function
*/
if (! preg_match ("/(.+)\n([0-9]{3}\-[0-9]{4})\n(Teen ([0-9]{3}\-[0-9]{4})\n)?(.+)\n\(([0-9]+)\)/", $string, $matches))
continue;
/* load the collected data into the $data array */
/* N.B index names should be included in quotes */
$data[$i]['name'] = $matches[1];
$data[$i]['number'] = $matches[2];
$data[$i]['teen'] = $matches[4];
$data[$i]['address'] = $matches[5];
$data[$i]['lot'] = $matches[6];
}
/* for demonstration purposes dump the $data array variable */
print_r ($data);
?><?php
$rawdata = Array ("Zeller, Charles & Eve
453-5432
2509 Jefferson Road
(265)",
* If the data in $string fails to conform to this format it will be skipped.
* The data collected from this expression is loaded into the array $matches by the preg_match() function
*/
if (! preg_match ("/(.+)\n([0-9]{3}\-[0-9]{4})\n(Teen ([0-9]{3}\-[0-9]{4})\n)?(.+)\n\(([0-9]+)\)/", $string, $matches))
continue;
/* load the collected data into the $data array */
$data[$i]['name'] = $matches[1];
$data[$i]['number'] = $matches[2];
$data[$i]['teen'] = $matches[4];
$data[$i]['address'] = $matches[5];
$data[$i]['lot'] = $matches[6];
}
/* for demonstration purposes dump the $data array variable */
print_r ($data);
?>