![]() |
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 |
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( 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 |
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 |
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 |
A VERY easy way to do this is to use parse_str() like this:
Code:
//assuming $input already has the data -Snib - http://www.snibworks.com Where will you be in 100 years? |
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 |
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.