Reputation: 355
I'm trying to get the contents of the second quotes and only the second quotes from a string. Right now I'm able to get the contents of all three quotes. What am I doing wrong? Is it possible to just print the second value in the output array?
Text
2014-06-02 11:48:41.519 -0700 Information 94 NICOLE Client "[WebDirect] (207.230.229.204) [207.230.229.204]" opening database "FMServer_Sample" as "Admin".
PHP
if (preg_match_all('~(["\'])([^"\']+)\1~', $line, $matches))
$database_names = $matches[2];
print_r($database);
Output
[WebDirect] (207.230.229.204) [207.230.229.204], FMServer_Sample, Admin
Upvotes: 2
Views: 339
Reputation: 786289
You can use it this way:
$re = '~(["\'])[^"\']+\1[^"\']*(["\'])([^"\']+)\1~';
$str = '2014-06-02 11:48:41.519 -0700 Information 94 NICOLE Client "[WebDirect] (207.230.229.204) [207.230.229.204]" opening database "FMServer_Sample" as "Admin".';
if ( preg_match($re, $str, $m) )
echo $m[3]; // FMServer_Sample
Upvotes: 3
Reputation: 47292
The match group starts at [0] and since you're using preg_match_all
you get [3] groups. The array is the first array of $matches, so it also starts at zero. You could use it like this:
if (preg_match_all('~(["\'])([^"\']+)\1~', $line, $matches))
$database_names = $matches[0][1];
print_r($database_names);
Upvotes: 0