Let us take the sample string of:
Code: Select all
blablabla[n option1=http://www.google.com option2=blabla]this is my string[/n]blablabla[n option2="bla2"]this is another string[/n]blablablaCode: Select all
1. [n option1=http://www.google.com]this is my string[/n]
2. [n option2="bla2"]this is another string[/n]So what I want to say is this:
Search for any character string followed by an equal sign following by a data string (which can be optionally surrounded by double quotes) and shove this data into an associative array with "key" as the association array key and the data string as the value within it
Using the following tcode is what I thought would isolate each option for me (needless to say, it didn't):
Code: Select all
preg_match_all('~([\w]+=["]??[\w:/\.\']["]??+)~', $str, $match);Code: Select all
preg_match_all('~([\w]+=[\w:/\.\']+)~', $str, $match);
echo $match[0][0]I am COMPLETELY confused and digging through regex and PHP docs hasn't helped me too much (not sure if I'm having a brain-dead day or I'm just not grasping the preg options). Any help/solution (with a brief explanation) would be greatly appreciated.
If I can at least get the option=whatever strings it should be easy enough to split...
To illustrate a little better, this "almost" does what I need it to:
Code: Select all
while ( strpos($str, '=', $lastOffset) !== false)
{
// key
$pos = strpos($str, '=', $lastOffset);
for ($keyPos = $pos; $keyPos >= 0; $keyPos--)
{
if ( strcmp($str[$keyPos], " ") == 0 )
break;
}
$key = substr($str, $keyPos, $pos - $keyPos);
// value
for ($valuePos = $pos; ; $valuePos++)
{
if ( strcmp($str[$valuePos], " ") == 0 || strcmp($str[$valuePos], "]") == 0 )
break;
}
$value = substr($str, $pos + 1, $valuePos - $pos - 1);
// set
$lastOffset = $pos + 1;
$data[$key] = $value;
}