Re: Help Needed with CSV & PHP
| From: | Jeff Gannaway | Date: | Tue, 04 Jul 2000 16:27:58 +0000 |
| Subject: | Re: Help Needed with CSV & PHP | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-4822@lists.php.net to get a copy of this message | ||
At 05:00 PM 7/4/00 -0400, Mike Gifford wrote:
>How will this avoid the problem of confusing what separates the
>different elements.
>
>$pizza = "$piece1:$piece2:$piece3:$piece4:$piece5:$piece6";
>$pieces = explode(":", $pizza);
>
>Will still be confused if a user enters in a : in $piece2 won't it?
You're right. I just tried this:
<?
$piece1 = "Pepporoni:Sausage";
$piece2 = "mushroom";
$piece3 = "cheese";
$piece4 = "bacon:onion:green pepper";
$piece5 = "thick crust";
$piece6 = "thin:crispy";
$pizza = "$piece1:$piece2:$piece3:$piece4:$piece5:$piece6";
$pieces = explode(":", $pizza);
print sizeof($pieces);
?>
and it came up with 10 elements in the $pieces array.
Here's the long, icky way I've dealt with similar problems with a Perl
script long ago...
Substitue ever occurence of : in the original variables with something
whacky that you'd expect no human being to EVER type in into an INPUT box
like:
<?
// First replace : with whacky code phrase
$piece1 = str_replace(":", "ColonColonColonColon", $piece1);
$piece2 = str_replace(":", "ColonColonColonColon", $piece2);
$piece3 = str_replace(":", "ColonColonColonColon", $piece3);
$piece4 = str_replace(":", "ColonColonColonColon", $piece4);
$piece5 = str_replace(":", "ColonColonColonColon", $piece5);
$piece6 = str_replace(":", "ColonColonColonColon", $piece6);
// Convert to array $pieces[]
$pizza = "$piece1:$piece2:$piece3:$piece4:$piece5:$piece6";
$pieces = explode(":", $pizza);
// Re-replace code phrase with :
$PiecesSize = sizeof($pieces);
$Counter = 0;
while ($Counter < $PiecesSize)
{
$pieces[$Counter] = str_replace("ColonColonColonColon", ":",
$pieces[$Counter]);
$Counter++;
}
// Now you have a 6-element array with their original :
?>
The only wierd thing is that there is still a chance some dork will enter
ColonColonColonColon or whater your whacky code phrase is and the end
result is that it got turned into a :
If you were REALLY concerned about that, you could check for occurences of
your code phrase before rplacing the : with it and if it does exist, then
add a varaible to "mark" which ones to ignore.
Out of breath,
Jeff Gannaway