SitePoint Sponsor

User Tag List

Results 1 to 13 of 13
  1. #1
    SitePoint Enthusiast
    Join Date
    Apr 2003
    Location
    Rome
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Regexp and [list] structure tags

    Hello anybody,my goal should be this:
    convert
    PHP Code:

    Some text
    some textsome textsome text

    [list=1][*]sdfsdfsd
    [*]sdfsdfsd
    [*]dsfsdfsdfsdsd
    [*]vvvvvvvvv
    [/list]
    some textsome textsome textsome textsome text 
    to this:
    PHP Code:
    Some textsome textsome textsome text

    [list]
    [
    sometext]sdfsdfsdfsd
    [sometext]sdfsdfsd
    [sometext]dsfsdfsdfsdsd
    [sometext]vvvvvvvvv[/list]some textsome textsome textsome textsome text 
    Now, i'm getting a bit of problems, playing around with regexps :
    PHP Code:
    $pattern "#\[list=\d\](\[\*\](.*?))*?\[/list\]#";
    echo 
    preg_replace($pattern,"[list][sometext]$2",$subjects); 
    Can you please, suggest something, will be very appreciate!!
    thank you in advance

  2. #2
    SitePoint Enthusiast
    Join Date
    Apr 2003
    Location
    Rome
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Please any help on this?
    thank you very much

  3. #3
    SitePoint Evangelist
    Join Date
    May 2004
    Location
    New Jersey, USA
    Posts
    567
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Is there some reason you can't just translate all the[*] into [sometext]? (Does[*] ever occur outside of list../list?)

    If not, try str_replace.

    If there is a reason, try doing it with string functions first (they're faster, and easier to debug):

    PHP Code:
    $lists preg_split('#\[/?list(?:=\d+)?\]#'$subjects);

    for (
    $i 0$i count($lists); $i++) {
      if (
    $i == 0)
        continue;

      
    $items explode('[*]'$lists[$i]);
      
    $lists[$i] = implode('[sometext]'$items);

    =Austin
    Austin Hastings - Principal Consultant - Longacre, Inc.

    Anything you can do, you can do better.

  4. #4
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by k4is3r
    Hello anybody,my goal should be this:
    convert
    PHP Code:

    Some text
    some textsome textsome text

    [list=1][*]sdfsdfsd
    [*]sdfsdfsd
    [*]dsfsdfsdfsdsd
    [*]vvvvvvvvv
    [/list]
    some textsome textsome textsome textsome text 
    In other words, you're trying to replace only within certain tags, right?
    The common approach is to use a regexp with two look-arounds, like this
    Code:
    SEARCH (?=( (?<!START_TAG).)* END_TAG)
    Example:
    PHP Code:
    $t = <<<EOS
    Some text, some text, some text, some text
    leave this * as is
    <list=1>
    * xxx
    * yyyy
    * zzzzz
    </list>
    some text,
    dont replace this *
    some text, some text
    EOS;

    $t preg_replace(
        
    "~\*(?=((?<!<list).)*</list>)~si"
        
    "HITHERE"
        
    $t); 

  5. #5
    Keep it simple, stupid! bokehman's Avatar
    Join Date
    Jul 2005
    Posts
    1,935
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by stereofrog
    Code:
    SEARCH (?=( (?<!START_TAG).)* END_TAG)
    In your example you are looking for a lone character "*". Could you give an example of: <![CDATA[<a></a>]]> how to capture the <a> elements in that example. I just can't get the code right to do that. Here is what I tried:
    PHP Code:
    <?php

    $ts 
    = <<<END
    words
    <tag> and </tag>
    text
    <![CDATA[ <tag> and </tag> ]]>
    words
    <tag> and </tag>
    text
    <![CDATA[ <tag> and </tag> ]]>
    words
    <tag> and </tag>
    text
    <![CDATA[ <tag> and </tag> ]]>
    words
    <tag> and </tag>
    text
    END;

    echo 
    preg_replace(
                   
    '~(?:\<([^>]*)>)(?=((?<!\<\!\[CDATA\[).)*\]\]\>)~si'
                   
    '&lt;$1&gt;',
                   
    $ts);
    ?>
    Why is this selecting the wrong "<"?

  6. #6
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The above regexp checks if given character is followed by the certain sequence of chars (namely tag content). When you apply it to strings, it means "some string followed by the tag content" and not "some string within the tag", in other words, only the last char of the string should be within tags, not the whole string. That's why in your example the first match is <![CDATA[ <tag> and not just <tag>.

  7. #7
    Keep it simple, stupid! bokehman's Avatar
    Join Date
    Jul 2005
    Posts
    1,935
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the expanation. I answered similar version of this question yesterday in this thread using a different method but I have, seen in the past, and your answer here demonstrates, your knowledge of PCRE is interesting as well as extensive.

    I guess I could use your method in array context as follows:
    PHP Code:
    $regex = array('~\<(?=((?<!<!\[CDATA\[).)*\]\]>)~si''~\>(?=((?<!<!\[CDATA\[).)*\]\]>)~si');
    $replacement = array('&lt;','$gt;');
    $t preg_replace($regex$replacement$t); 

  8. #8
    SitePoint Enthusiast
    Join Date
    Apr 2003
    Location
    Rome
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by stereofrog
    In other words, you're trying to replace only within certain tags, right?
    The common approach is to use a regexp with two look-arounds, like this
    Code:
    SEARCH (?=( (?<!START_TAG).)* END_TAG)
    Example:
    PHP Code:
    $t = <<<EOS
    Some text, some text, some text, some text
    leave this * as is
    <list=1>
    * xxx
    * yyyy
    * zzzzz
    </list>
    some text,
    dont replace this *
    some text, some text
    EOS;

    $t preg_replace(
        
    "~\*(?=((?<!<list).)*</list>)~si"
        
    "HITHERE"
        
    $t); 

    You are right, but i want to leave the <list=1> </list> tag, after replacing.
    SO, as you know, i want to replace the "[*]" tag with "something" only if it is contained in a <list=1></list> tag.
    thank you again

  9. #9
    Keep it simple, stupid! bokehman's Avatar
    Join Date
    Jul 2005
    Posts
    1,935
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by k4is3r
    You are right, but i want to leave the <list=1> </list> tag, after replacing.
    Those elements are present but you will need to view the source to see them.

  10. #10
    SitePoint Enthusiast
    Join Date
    Apr 2003
    Location
    Rome
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by bokehman
    Those elements are present but you will need to view the source to see them.
    And how o print the source code? ($r variable for intend)
    thank you

  11. #11
    Keep it simple, stupid! bokehman's Avatar
    Join Date
    Jul 2005
    Posts
    1,935
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well you would right click in your browser and select view source.

  12. #12
    SitePoint Enthusiast
    Join Date
    Apr 2003
    Location
    Rome
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by bokehman
    Well you would right click in your browser and select view source.
    Yes, i know :P, but i don't know how to print ( with print() or echo()) the real SOURCE on my page!

  13. #13
    Keep it simple, stupid! bokehman's Avatar
    Join Date
    Jul 2005
    Posts
    1,935
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    with htmlspecialchars();


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •