|
|
|
#1 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
<otrWalter@gmail.com> wrote in message news:1194550910.202651.104320@t8g2000prg.googlegro ups.com... > 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 wait, wait, wait. so you are opening what apparently is a php script and parsing it? try: /new\s+wBug\s?[^(]*\(\s*([^,)]*)[,)]/i |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
On Thu, 08 Nov 2007 20:41:50 +0100, otrWalter@gmail.com
<otrWalter@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. First of all, are you sure regex is the way to go here? It looks awfully like a parser should be involved... And while it's perfectly doable to create a regex for it, why not apply KISS and: if(preg_match('/\bnew wBug\s*\(\s*(.+)\s*\);/i', $code, $arrMatches)){ $results = $explode(',',arrMatches); array_walk($results,'trim'); } It's a LOT easier to maintain then a regex... -- Rik Wasmus |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
> It's a LOT easier to maintain then a regex...
> -- > Rik Wasmus Thanks Rik! Walter |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
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"; } |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
<mik3l3374@gmail.com> wrote in message news:1194715821.462826.259940@k35g2000prh.googlegr oups.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. |
|
![]() |
| Outils de la discussion | |
|
|