Noob here need help!!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
theoretical_dreamer
Forum Newbie
Posts: 13
Joined: Mon May 22, 2006 12:53 am

Noob here need help!!

Post 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
Last edited by theoretical_dreamer on Mon May 29, 2006 9:55 pm, edited 1 time in total.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post 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().
Last edited by Christopher on Mon May 29, 2006 9:46 pm, edited 1 time in total.
(#10850)
theoretical_dreamer
Forum Newbie
Posts: 13
Joined: Mon May 22, 2006 12:53 am

Post by theoretical_dreamer »

i'll try it thanks :D
User avatar
sweatje
Forum Contributor
Posts: 277
Joined: Wed Jun 29, 2005 10:04 pm
Location: Iowa, USA

Post 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
theoretical_dreamer
Forum Newbie
Posts: 13
Joined: Mon May 22, 2006 12:53 am

Post by theoretical_dreamer »

please elaborate im a newbie @ php tnx :D
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post 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.
Post Reply