Another RegExp challenge...

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
visionmaster
Forum Contributor
Posts: 139
Joined: Wed Jul 14, 2004 4:06 am

Another RegExp challenge...

Post 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!
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

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