Reputation: 1680
I have the following string "<?php jifoafj sfjifasjfoasjfifajs ReadThis(array('type' => 'button', 'value' => 35)) oisfjoijsiofsa sffas ?>"
I am trying to get whatever's inside ReadThis.
This is the Regex so far,
preg_match_all('~<\?(?:php)?\s+ReadThis\(([^)]+)\).*?\?>~s',$thestring,$matches) ;
Ok, but I am getting
array('type' => 'button', 'value' => 35
what I want is
array('type' => 'button', 'value' => 35)
What is the best approach to accomplishing this? Does this have something todo with the Chomsky Hierarchy?
Upvotes: 1
Views: 114
Reputation: 15000
This regex will search your sample string and find the ReadThis and the contents of the outer most set of parans.
<[?](?:php\b)?.*?\s\bReadThis[(]([^)]+[)][^)]*)[)].*?[?]>
Group 0 will receive the entire string, and Group 1 will have the readthis through the last paran.
I modified your search:
\b
after php
and before readthis
to ensure you don't accidentally find a readthis
as part of a larger string<?php
$sourcestring="<?php jifoafj sfjifasjfoasjfifajs ReadThis(array('type' => 'button', 'value' => 35)) oisfjoijsiofsa sffas ?>";
preg_match_all('/<[?](?:php\b)?.*?\s\bReadThis[(]([^)]+[)][^)]*)[)].*?[?]>/im',$sourcestring,$matches);
echo "<pre>".print_r($matches,true);
?>
$matches Array:
(
[0] => Array
(
[0] => <?php jifoafj sfjifasjfoasjfifajs ReadThis(array('type' => 'button', 'value' => 35)) oisfjoijsiofsa sffas ?>
)
[1] => Array
(
[0] => array('type' => 'button', 'value' => 35)
)
)
Upvotes: 0
Reputation: 89557
You can use this pattern:
ReadThis(\((?:[^()]++|(?1))+\))
Example:
$pattern = '~ReadThis(\((?<result>(?:[^()]++|(?1))+)\))~';
$subject = <<<'LOD'
< ?php jifoafj sfjifasjfoasjfifajs ReadThis(array('type' => 'button', 'value' => 35)) oisfjoijsiofsa sffas sdfsdf sd
ReadThis(array('type' => 'button', 'value' => 35), 'foo') ? >
LOD;
preg_match_all($pattern, $subject, $matches);
print_r($matches['result']);
you obtain:
array('type' => 'button', 'value' => 35)
array('type' => 'button', 'value' => 35), 'foo'
Upvotes: 1
Reputation: 11610
It looks to me like your pattern correctly matches the full string, but has the ending paren consumed outside of your capture group... Just move the escape sequence for that paren inside of the capture group instead:
<\?(?:php)?\s+ReadThis\(([^)]+\)).*?\?>
(where your original regex reads this; note the moved backslash)
<\?(?:php)?\s+ReadThis\(([^)]+)\).*?\?>
Upvotes: 1
Reputation: 46
This regex doesn't match anything on this string.
Anyway, to fix your issue juste replace [^)] by a dot like :
~<\?(?:php)?\s+ReadThis\((.+)\).*?\?>~s
Upvotes: 1
Reputation: 324630
You don't have to match the entire string.
(ReadThis\((array\([^)]+\))\))
The important thing is to capture that closing parens.
Upvotes: 1