|
Posted by Steve on 11/12/07 02:02
<mik3l3374@gmail.com> wrote in message
news:1194715821.462826.259940@k35g2000prh.googlegroups.com...
> On Nov 9, 3:41 am, "otrWal...@gmail.com" <otrWal...@gmail.com> wrote:
>> Note: $code is a single line of code that the previous segment of this
>> method has located.
>>
>> I have this...
>>
>> preg_match('/\bnew wBug\s*\(\s*(.+)\s*\);/i', $code, $arrMatches);
>> $results = $arrMatches[1];
>>
>> it will find this...
>>
>> new wBug ( $myvar ); // <-- this is what $code contains
>>
>> and it will give me...
>>
>> $myvar
>>
>> Which is exactly what I want, for this instance, but if I have this...
>>
>> new wBug ( $myvar, true ); // <-- this is what $code contains,
>> this time
>>
>> I get this...
>>
>> $myvar, true
>>
>> I'd like to only get....
>>
>> $myvar
>>
>> Actually, I'd like to get each parameter in its own array element of
>> '$arrMatches'
>>
>> My RegExp is limited on this one.
>>
>> Can someone throw me a bone?
>>
>> Thx
>>
>> walter
>
> how about this, no regexp, just simple sub strings
>
> $string = 'new wBug ( $myvar , true )';
> $firstpos = strpos($string,"(");
> $lastpos = strrpos($string,")");
> $str=substr($string,$firstpos+1,$lastpos-$firstpos-1);
> if ( strstr($str,",") )
> {
> $var=explode(",",$str);
> echo $var[0];
> }
> else
> {
> echo "$str\n";
> }
usually a great way to go. however when parsing, there's no good way to get
around the literal difference v. abstract differences between 'new wBug (
$myVar' or 'new wbug($myVar' or any other variation. all are the same
when seen by php, but not by strpos - which looks for literal string
occurances. regex can more easily account for any variation of 'new wBug'
and return exactly what the op is looking for.
Navigation:
[Reply to this message]
|