Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > Pro PHP
|
Pro PHP Advanced PHP coding discussions. Beginning-level questions will be redirected to the Beginning PHP forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Pro PHP section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 13th, 2004, 05:32 AM
Authorized User
 
Join Date: Oct 2004
Posts: 84
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to SiliconFuRy
Default 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
__________________
Many shoes,

Jamez/SiliconFuRy
 
Old December 13th, 2004, 07:14 AM
Authorized User
 
Join Date: Dec 2004
Posts: 44
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to colin.horne
Default

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
 
Old December 13th, 2004, 08:00 AM
Authorized User
 
Join Date: Oct 2004
Posts: 84
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to SiliconFuRy
Default

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
 
Old December 13th, 2004, 09:07 AM
Authorized User
 
Join Date: Oct 2004
Posts: 84
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to SiliconFuRy
Default

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
 
Old December 13th, 2004, 01:29 PM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 1,285
Thanks: 0
Thanked 2 Times in 2 Posts
Default

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?
 
Old December 14th, 2004, 06:33 AM
Authorized User
 
Join Date: Oct 2004
Posts: 84
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to SiliconFuRy
Default

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
 
Old December 15th, 2004, 06:43 AM
Authorized User
 
Join Date: Dec 2004
Posts: 44
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to colin.horne
Default

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





Similar Threads
Thread Thread Starter Forum Replies Last Post
Parsing from a text string jroxit Classic ASP Basics 5 November 18th, 2008 05:08 PM
Parsing a text string medix_911 Access 8 November 21st, 2006 01:37 PM
parsing a changing text file on the fly Kannkor VB How-To 0 November 14th, 2005 01:47 AM
HTML block in Text field ndisdabest HTML Code Clinic 2 August 16th, 2005 02:25 PM
Parsing TEXT files - VERY HARD!! cplusruss Pro VB 6 2 March 24th, 2005 05:55 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.