Page 1 of 1
Noob here need help!!
Posted: Mon May 29, 2006 9:34 pm
by theoretical_dreamer
I had a search form but I have to separate the values that was inputted by the user. For example
the user inputs m12ws001. How can I separate this query into 4 separate field in the database?
Example:
m12ws001 -----> m=branch,12=floor, ws=machine, 001-host number
Thanks and more power
Posted: Mon May 29, 2006 9:40 pm
by Christopher
Are the fields fixed width or do they vary? If they are fixed width you can use substr(). If they can be different lengths the use preg_split().
Posted: Mon May 29, 2006 9:43 pm
by theoretical_dreamer
i'll try it thanks

Posted: Mon May 29, 2006 9:47 pm
by sweatje
One way to approch this would be to parse the string using a regular expression. I am taking a little liberty to define the problem space by assuming the branch will be one or more characters, followed by the floor will always be numeric, followed by the machine always being one or more characters and the host being numeric. Assuming these rules are realistic, the following
SimpleTest test case shows how to parse the string and verifies the results:
Code: Select all
function testRegexParsingString() {
$str = 'm12ws001';
preg_match(
'/(?P<branch>[a-z]+)(?P<floor>\d+)(?P<machine>[a-z]+)(?P<host>\d+)/i'
,$str
,$match);
$this->assertEqual('m', $match['branch']);
$this->assertEqual('12', $match['floor']);
$this->assertEqual('ws', $match['machine']);
$this->assertEqual('001', $match['host']);
}
HTH
Posted: Mon May 29, 2006 9:54 pm
by theoretical_dreamer
please elaborate im a newbie @ php tnx

Posted: Mon May 29, 2006 10:19 pm
by Ambush Commander
Follow the link. When you do things like string parsing, it's a good idea to have a few unit tests handy.