p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   Pro PHP (http://p2p.wrox.com/forumdisplay.php?f=96)
-   -   Parsing a block of text (http://p2p.wrox.com/showthread.php?t=22490)

SiliconFuRy December 13th, 2004 05:32 AM

Parsing a block of text
 
Schoochies...

I wanna basically turn a block of text like this...
----------
Name=Smeghead

Age=20

Location=Boeblingen, Germany
----------

Into variables like this...

$name="Smeghead"
$age=20
$location="Boeblingen, Germany"

Any pointers on how to go through a long string like above and parse it for things like above?


Many shoes,

Jamez/SiliconFuRy

colin.horne December 13th, 2004 07:14 AM

Just a quick security pointer:

Doing this will allow ANYONE with access to that file to create variables within your PHP script, so if (for example), you have a variable - $is_logged_in - then someone can do this:

Name = Smeghead
Age = 20
Location = Boeblingen, Germany
is_logged_in = 1

You might want to do something like this:

Code:

$allowed_variables = array(
  "name",
  "age",
  "location"
);

Even if only certain people are allowed to access the file, doing that will also reduce the chances of someone breaking the script with a typo.

Anyway, the answer to your question is: use regular expressions.

Something like this ought to do:
/([^=]+)=(.+)/

Look up for regular expressions in the php manual, and the function needed is: php.net/preg_match.

If you still need a hand, shout :-)

Cheers

--
Please contact me at:
Colin (dot) Horne (at) gmail (dot) com

SiliconFuRy December 13th, 2004 08:00 AM

hey colin,

Thanks for the info.

Ye im aware of the security implications. My code will have to be very robust, its part of a system i think I will have to design (doing research for a future project), that will allow data to be inputted via email into a database, and the email will have to be parsed very strictly, and do all sorts of error checking and feedback.

I don't think something like thats been done before, if any1 knows someone who has, gimmi a url ;)

Many shoes,

Jamez/SiliconFuRy

SiliconFuRy December 13th, 2004 09:07 AM

Finally got a piece of example code working, along with a little regex help from a friend of mine... heres what i got working...

<?php
$str = "name=james love\nage=20\nface=cake\nshoes=happy";
$matches = Array();
preg_match_all("/([a-z_][a-z0-9]*)=(.+?)/iU", $str,$matches);
// /([a-z_][a-z0-9]*)=(.+?)/iU
// /([^=]+)=(.+)/

print_r($matches);

$allowed_vars = Array("name","age","face");

foreach($matches[1] as $k=>$v) {
        if(!in_array($v,$allowed_vars)) {
                echo "Variable name: " .
                $v . " not allowed, skipping...\n";
                continue;
        }
        $$v = $matches[2][$k];
}

echo "name: " . $name;
echo "\n age: : " . $age;
echo "\n face: " . $face;

?>

this, as you see, also succesfully sanitizes the variables, so "shoes" isnt allowed and is not converted to a variable.

Thanks again

Many shoes,

Jamez/SiliconFuRy

Snib December 13th, 2004 01:29 PM

A VERY easy way to do this is to use parse_str() like this:

Code:

//assuming $input already has the data

$input = str_replace("\n","&",$input);

parse_str($input);

hth,

-Snib - http://www.snibworks.com
Where will you be in 100 years?

SiliconFuRy December 14th, 2004 06:33 AM

Ah, cunning, nice one Snib. I do believe that works faster than the regex stuff... had to modify the functions bit from your examples, but heres how it works, using parse_Str....

<?php
$str = "name=james love\nage=20\nface=cake\nshoes=happy";
$matches = Array();
$str = str_replace("\n","&",$str);
$str = str_replace(" ", "+",$str);

parse_Str($str,$matches);

//print_r($matches);

$allowed_vars = Array("name","age","face");

foreach($matches as $k=>$v) {
        if(!in_array($k,$allowed_vars)) {
            echo "Variable name: " . $k .
            " not allowed, skipping...\n";
            continue;
        }
        $$k = $matches[$k];
}

echo "name: " . $name;
echo "\n age: : " . $age;
echo "\n face: " . $face;

?>

Many shoes,

Jamez/SiliconFuRy

colin.horne December 15th, 2004 06:43 AM

Heh, parse_str... Much better :-)

Possibly use rawurlencode & rawurldecode though, instead of simply taking out the spaces.

As it is atm, if you try to get back the origional text (with spaces, after you've parsed it into varaibles), you wont know which underscores where actually spaces...

Cheers

--
Please contact me at:
Colin (dot) Horne (at) gmail (dot) com


All times are GMT -4. The time now is 06:01 PM.

Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.