ashly Posted December 21, 2006 Share Posted December 21, 2006 Hi,I am working with PHP 5.0 . I have a string like this:[code][somecode] function getSize($userIDArr) { $arrSize = sizeof($userIDArr); } [somecode] function getUserNames($userIDArr) { for($i=0; $i < sizeof($userIDArr); ++$i) { $userNamesArr[$i] = $userIDArr[$i]; } return $userNamesArr; } [/somecode][/somecode][/code]I need to replace only the outer tag pair [somecode] [/somecode] with <abc> </abc>The result should look like:[code]<abc> function getSize($userIDArr) { $arrSize = sizeof($userIDArr); } [somecode] function getUserNames($userIDArr) { for($i=0; $i < sizeof($userIDArr); ++$i) { $userNamesArr[$i] = $userIDArr[$i]; } return $userNamesArr; } [/somecode]</abc>[/code]If anyone have any idea, please help me to solve this.Thanks in advanceAshly Quote Link to comment Share on other sites More sharing options...
c4onastick Posted December 21, 2006 Share Posted December 21, 2006 This is actually pretty tricky with regex. Its really hard to matched balanced sets of delimiters. Depending on the rest of the structure, this is a pretty weak solution but it may work for you:[code]$newcode = preg_replace('/\[somecode\](.*)\[\/somecode\]/s', '<abc>\1<\/abc>', $oldcode);[/code]That will match the outermost (highest level) "somecode" tags and replace them with the 'abc' tag. But it will also match here:[code][somecode] <= MATCH ...[/somecode][somecode] More Code [somecode] ... [/somecode][/somecode][somecode]...[/somecode] <= MATCH[/code]Without perl's 'depth' functionality its next to impossible to do this to an arbitrary depth. Quote Link to comment Share on other sites More sharing options...
ashly Posted December 21, 2006 Author Share Posted December 21, 2006 HiThat's Great... thanks you so much for the solution..That works as you explained.. but again there is some problem arises when i use the code like this: [code][somecode] function getSize($userIDArr) { $arrSize = sizeof($userIDArr); } [somecode] function getUserNames($userIDArr) { for($i=0; $i < sizeof($userIDArr); ++$i) { $userNamesArr[$i] = $userIDArr[$i]; } return $userNamesArr; } [/somecode] [/somecode] ------------------------ some comment in between ------------------------ [somecode] function getCustomerNames($custIDArr) { for($j=0; sss < sizeof($custIDArr); ++j) { $customerNameArr[$j] = $custIDArr[$j]; } return $customerNameArr; } [/somecode] [/code] but I need the result as follows: [code]<abc> function getSize($userIDArr) { $arrSize = sizeof($userIDArr); } [somecode] function getUserNames($userIDArr) { for($i=0; $i < sizeof($userIDArr); ++$i) { $userNamesArr[$i] = $userIDArr[$i]; } return $userNamesArr; } [/somecode] </abc> ------------------------ some comment in between ------------------------ [abc] function getCustomerNames($custIDArr) { for($j=0; sss < sizeof($custIDArr); ++j) { $customerNameArr[$j] = $custIDArr[$j]; } return $customerNameArr; } [/abc] [/code] is there any solution for this? ThanksAshly Quote Link to comment Share on other sites More sharing options...
effigy Posted January 2, 2007 Share Posted January 2, 2007 Are you always basing this on one level of nesting? If so, you can add this to your pattern to make it more specific. Otherwise, you could split the data apart using the [tt] somecode[/tt]'s as your delimiter, but saving it in the process. You'll then have an array of somecodes and everything elses, which you can loop through and keep track of the nesting levels. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.