Page 1 of 1

Another RegExp challenge...

Posted: Wed Jun 29, 2005 6:17 am
by visionmaster
Hello together,

I have following string:

productname#ptb#usage#pte#characteristic

=> as a result i want a result array like this:
$arrProduct["productname"] = productname
$arrProduct["usage"] = usage
$arrProduct["characteristic"] = characteristic

or a string like this:

productname#ptb#usage
=> as a result i want a result array like this:

$arrProduct["productname"] = productname
$arrProduct["usage"] = usage
$arrProduct["characteristic"] = ''


productname#pte#characteristic

=> as a result i want a result array like this:
$arrProduct["productname"] = productname
$arrProduct["usage"] = ''
$arrProduct["characteristic"] = characteristic

I guess the best way is to use RegExp. I have here RegexBuddy, but I have no idea where to start from... :-(

Thanks for your help!

Posted: Wed Jun 29, 2005 6:38 am
by CoderGoblin
The manual has some good examples and descriptions. I would recommend the following pages:

Pattern Syntax
preg_match (with the array &matches included).

You could also use the function explode

Code: Select all

$final_array=array("col1"=>"","col2"=>"","col3"=>"","col4"=>"");
$input="col1#col2#col3#col4";
$base_array=explode("#",$input);
foreach($base_array as $value) {
  $final_array[$value]=$value;
}
Hope that helps

Quick Edit: Unless you add some way of knowing which columns are missing you will run into problems.
How does a computer know which columns are used with the following data if the columns are "values" not column names ?
col1#col2#col5#col6
col1#col4#col5#col6
It would be better to have col1#col2###col5#col6 and col1###col4#col5#col6.