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

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
ashly
Forum Newbie
Posts: 9
Joined: Thu Dec 21, 2006 12:14 am

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

Post 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]
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post 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
ashly
Forum Newbie
Posts: 9
Joined: Thu Dec 21, 2006 12:14 am

Post 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]
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post 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
User avatar
Fractal
Forum Commoner
Posts: 54
Joined: Tue Aug 16, 2005 1:28 pm

Post by Fractal »

Try making the middle tag optional.

Code: Select all

preg_replace("/\[[".$tag."]\].*(\[[".$tag."]\])?(.*)(\[\/[".$tag."]\])?\[\/[".$tag."]\].*/s", "<tag>\\1\\2\\3</tag>", $code);
ashly
Forum Newbie
Posts: 9
Joined: Thu Dec 21, 2006 12:14 am

Post by ashly »

Hi

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

Thanks
Ashly
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

strpos() is a loop.
Post Reply