Page 1 of 1

Replace only the outer tag if nested tag exists in a reg exp

Posted: Thu Dec 21, 2006 12:28 am
by ashly
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hi,
I am working with PHP 5.0 . I have a string like this:

Code: Select all

[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]

I need to replace only the outer tag pair [somecode] [/somecode] with <abc> </abc>

The result should look like:

Code: Select all

<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>

If anyone have any idea, please help me to solve this.

Thanks in advance
Ashly


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Thu Dec 21, 2006 2:52 am
by Kieran Huggins
This works for me:

Code: Select all

echo preg_replace('/\[somecode\](.*\[somecode\].*\[\/somecode\].*)\[\/somecode\]/s','<abc>$1</abc>',$code);
You could make it even more re-usable like this:

Code: Select all

$tag = 'somecode';

echo preg_replace('/\['.$tag.'\](.*\['.$tag.'\].*\[\/'.$tag.'\].*)\[\/'.$tag.'\]/s','<abc>$1</abc>',$code);
Cheers,
Kieran

Posted: Thu Dec 21, 2006 4:34 am
by ashly
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hi Kieran

Great...  thanks you so much Kieran..
That works for me..  but again there is some problem arises when i use the code like this:

Code: Select all

[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]


but I need the result as follows:

<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]

Any idea about this?

Thanks
Ashly


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Thu Dec 21, 2006 10:31 am
by Kieran Huggins
I'm fairly stumped on that one for now... anyone else have an idea?

I'll take another look later today if it isn't solved yet.

Cheers,
Kieran

Posted: Thu Dec 21, 2006 11:25 am
by Fractal
Try making the middle tag optional.

Code: Select all

preg_replace("/\[[".$tag."]\].*(\[[".$tag."]\])?(.*)(\[\/[".$tag."]\])?\[\/[".$tag."]\].*/s", "<tag>\\1\\2\\3</tag>", $code);

Posted: Sun Dec 24, 2006 1:55 am
by ashly
Hi

It doesn't work out the solution.
I'm fairly stucked up.
Anyone have any idea?

Thanks
Ashly

Posted: Tue Dec 26, 2006 8:11 am
by feyd
strpos() is a loop.