preg_match - match data with an "anything except"
Posted: Sat May 27, 2006 2:59 pm
So after my previous post today was answered (viewtopic.php?t=49115), I decided to continue moving on my quest of formatting/isolating string-based options in my script. Now that I have all the "tag" strings isolated, I want to extract the specified options from within them. I have made some preg attempts, but all have failed me.
Let us take the sample string of:
Let us assume that the following strings have been isolated:
I want to extract the option1 and option2 data.
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):
The closest I have come is:
However, that only gives me "option1=http://www.google.com" and "option2="bla2"".
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:
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;
}