Jump to content

How to replace only the outer tag if nested tag exists in a regular expression


ashly

Recommended Posts

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 advance
Ashly
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.
Hi
That'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?

Thanks
Ashly
  • 2 weeks later...
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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.