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
Noob here need help!!
Moderator: General Moderators
-
theoretical_dreamer
- Forum Newbie
- Posts: 13
- Joined: Mon May 22, 2006 12:53 am
Noob here need help!!
Last edited by theoretical_dreamer on Mon May 29, 2006 9:55 pm, edited 1 time in total.
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
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().
Last edited by Christopher on Mon May 29, 2006 9:46 pm, edited 1 time in total.
(#10850)
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:
HTH
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']);
}
-
theoretical_dreamer
- Forum Newbie
- Posts: 13
- Joined: Mon May 22, 2006 12:53 am
- Ambush Commander
- DevNet Master
- Posts: 3698
- Joined: Mon Oct 25, 2004 9:29 pm
- Location: New Jersey, US