perl regex - Now in PHP!

Bryce C plug-discuss@lists.plug.phoenix.az.us
26 Mar 2003 15:13:09 -0700


--=-Nr+wK7xKpthMjp1XFSw+
Content-Type: text/plain
Content-Transfer-Encoding: quoted-printable

Here's my PHP script that will generate a CSV file from your data in
FILE. I've tested it on the data you sent and it does work. Also, make
sure there are no line breaks in the lines or that may break things.

<?php
$arr=3Dfile("FILE");     //Break the data into each line
for($i=3D0;$i<sizeof($arr);$i++)  //For every line
{
        $arr[$i]=3Dexplode(":", $arr[$i]);
        for($j=3D0;$j<sizeof($arr[$i]);$j++)
        {
                $arr[$i][$j]=3Dexplode(",", $arr[$i][$j]);
                $arr[$i][$j]=3D$arr[$i][$j][0];
                $arr[$i][$j]=3Dsubstr($arr[$i][$j], 1);
        }
        array_shift($arr[$i]);
}

//CSV now
$fp=3Dfopen("testParse.csv", "w");
$header=3D'"TYPE", "SUBTYPE", "ID", "LICENSE", "NAME", "LEGALLY",
"ADDRESS", "CITY", "CITY", "STATE", "ZIP", "TELEPHONE", "FAX",
"CAPACITY", "COUNTY"';
$header.=3D"\n";
fputs($fp, $header);
for($i=3D0;$i<sizeof($arr);$i++)
{
        for($j=3D0;$j<sizeof($arr[$i])-1;$j++)
        {
                fputs($fp, '"'.$arr[$i][$j].'", ');
        }
      =20
$arr[$i][sizeof($arr[$i])-1]=3Dsubstr($arr[$i][sizeof($arr[$i])-1],0,-1);
        fputs($fp, '"'.$arr[$i][sizeof($arr[$i])-1].'"');
        fputs($fp, "\n");
}
fclose($fp);
?>



On Wed, 2003-03-26 at 14:49, David A. Sinck wrote:
> \_ SMTP quoth Mike Starke on 3/26/2003 16:33 as having spake thusly:
> \_
> \_ David is right, I probably should have included more accurate
> \_ data than my original message.
> \_=20
> \_ Here is a line:
> \_ ---------------------------------------------------
> \_ TYPE: AL ADHC, SUBTYPE: ADULT DAY HEALTH CARE, ID: AL2905, LICENSE: AD=
HC-2838, NAME: MARY'S PLACE ADULT DAY CENTER, LEGALLY: MARY'S PLACE ADULT D=
AY CENTER, ADDRESS: 14601 DEL WEBB  BLVD, CITY: SUN CITY, STATE: AZ, ZIP: 8=
5351, TELEPHONE: (623)584-4999, FAX: , CAPACITY: , COUNTY: MARICOPA
> \_ ---------------------------------------------------
>=20
> $line =3D qq{TYPE: ... from above ... };
>=20
> my @parts =3D split /, (\w+): /, $line;
> splice @parts, 0, 1, split /:/, $parts[0], 2;
> my %info =3D @parts;
>=20
> foreach (sort keys %info)
>   {
>     print "$_ =3D> $info{$_}\n";
>   }
>=20
> YMMV.  Yes, the splice is masking a weakness in the RE. :-)
>=20
>=20
> David
> ---------------------------------------------------
> PLUG-discuss mailing list - PLUG-discuss@lists.plug.phoenix.az.us
> To subscribe, unsubscribe, or to change  you mail settings:
> http://lists.PLUG.phoenix.az.us/mailman/listinfo/plug-discuss
--=20
Bryce C <Plug@BryceCo.Net>
CoBryce Communications

--=-Nr+wK7xKpthMjp1XFSw+
Content-Type: application/pgp-signature; name=signature.asc
Content-Description: This is a digitally signed message part

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.6 (GNU/Linux)
Comment: For info see http://www.gnupg.org

iD8DBQA+giX1/wbq/C6yyPcRAquZAKCojFWBn1A5s+pyJHHBjWGO8kBqQgCbBMPZ
HeP1W5JPWMssNgQJQFminxE=
=X2Q4
-----END PGP SIGNATURE-----

--=-Nr+wK7xKpthMjp1XFSw+--