Re: Help with regex

From: Date: Sun, 20 Jan 2002 19:08:22 +0000
Subject: Re: Help with regex
References: 1  Groups: php.windows 
Request: Send a blank email to php-windows+get-11534@lists.php.net to get a copy of this message
Michael, First off, you can eliminate the right-left stuff with the regex. Use: ereg("%ial\(([^[:blank:]]{1,},[0-9]{1,})\)",$text,$temp) Adding the parentheses around your function arguments lets you reference your "string,num" pattern directly with $temp[1]. For that matter, just use: ereg("%ial\([:blank:]*([^[:blank:])]+)[:blank:]*,[:blank:]*([^[:blank:])]+)[ :blank:]*\)",$text,$temp) This may look ugly, but it allows you to reference each argument using $temp[1] and $temp[2], without exploding or whatever. Then, just verify $temp[2] with is_numeric($temp[2]), and cast it to the type you want (e.g. (integer) $temp[2]). The [^[:blank:])]+ grabs one or more non-blank, non-right-paren characters. The exclusion of right-paren prevents it from trying to match the closing paren in your function. The [:blank:]* allows for some whitespace padding, without adding to what you're actually looking for. So you'd have: while (ereg("%ial\([:blank:]*([^[:blank:])]+)[:blank:]*,[:blank:]*([^[:blank:])]+) [:blank:]*\)",$text,$temp)) { if (is_numeric($temp[2])) $num = (integer) $temp[2]; else continue; $text = str_replace($temp[0],ial_search($temp[1],$num),$text); } It's important to note that doing things this way, you cannot have _any_ right parentheses in the arguments of %ial. You could add pieces to your regex to match more parens, but there is actually no way to do arbitrary nesting in regex. You need a parser for that. John "Michael Reynolds" <wshs_chat@yahoo.com> wrote in message news:20020119055403.98914.qmail@web11608.mail.yahoo.com... > I have something in php, which goes through a line of > text, replacing all occurences of %longip($a) with the > long2ip/ip2long of whatever's in the perentheses. I > tried something similar with %ial($a,$b), to call > ial_search($a,$b) and replace the %ial() with the > result. I have it so the longip can be stacked: > %longip(%longip(%longip($a))) > and it would call the proper function(s) 3 times. I'm > trying to get it to do the same with %ial. The %ial > takes a string as the first arg, and a number as the > second. If the second is 0, it returns a number. > What I have so far is: > > while > (ereg("%ial\([^[:blank:]]{1,},[0-9]{1,}\)",$text,$temp)) > { $ialsearch = > explode(",",right(left($temp['0'],-1),-5)); $text = > > str_replace($temp['0'],ial_search($ialsearch['0'],$ialsearch['1']),$text); > } > > Any help will be greatly appreciated. > > __________________________________________________ > Do You Yahoo!? > Send FREE video emails in Yahoo! Mail! > http://promo.yahoo.com/videomail/

« previous php.windows (#11534) next »