SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Evangelist ucahg's Avatar
    Join Date
    Apr 2001
    Location
    Sarnia, Ontario, Canada
    Posts
    434
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Regular Expression - Any character(s) code

    Is there a regular expression code that stands for any characters?

    I know the period stands for a character, but then the following code produces problems:

    PHP Code:
    <?PHP
    $output 
    "[UL][LI]test[/LI][LI]test[/LI][/UL]";

    $output eregi_replace("\[UL](.*)\[/UL]","<UL>\\1</UL>",$output);
    $output eregi_replace("\[OL](.*)\[/OL]","<OL>\\1</OL>",$output);
    $output eregi_replace("\[LI](.*)\[/LI]","<LI>\\1</LI>",$output);

    echo 
    $output;
    ?>
    The HTML code outputted is this:

    <UL><LI>test[/LI][LI]test</LI></UL>
    As you can see it doesn't stop at the first closing [/LI] tag. I either need another regexp symbol that will work for any character(s), or a group of regular expressions that will work for ANY character(s).

    Thanks.

  2. #2
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If there are not going to be any [UL] used in the actual content of the of the variable, you can do aq straight str_replace();

    PHP Code:
    <?
    $output 
    "[UL][LI]test[/LI][LI]test[/LI][/UL]";
    $in = array("[UL]""[/UL]""[OL]""[/OL]""[LI]""[/LI]");
    $out = array("<UL>""</UL>""<OL>""</OL>""<LI>""</LI>");
    $output str_replace($in$out$output);
    ?>
    If you really need the regualr expressions we cna work on that to.
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  3. #3
    SitePoint Evangelist ucahg's Avatar
    Join Date
    Apr 2001
    Location
    Sarnia, Ontario, Canada
    Posts
    434
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, I'd prefer it so if you don't have a closing tag, it doesn't show up (in which case regular expressions would be ideal). The formatting should be extremely user-friendly and easy to learn, and I don't want people asking me why something isn't working correctly.

  4. #4
    You talkin to me? Anarchos's Avatar
    Join Date
    Oct 2000
    Location
    Austin, TX
    Posts
    1,438
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Add a ? after the (.*) to make it "ungreedy".
    ck :: bringing chris to the masses.

  5. #5
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Okay then

    Code:
    $output = "[OL][LI]test[/LI][LI]test[/LI][/OL][UL][LI]test[/LI][LI]test[/LI][/UL]";
    $output = eregi_replace("(\[LI\]([^/LI]*)\[/LI\])","<LI>\\2</LI>",$output);
    $output = eregi_replace("(\[OL\](.*)\[/OL\])","<OL>\\2</OL>",$output);
    $output = eregi_replace("(\[UL\](.*)\[/UL\])","<UL>\\2</UL>",$output);
    echo $output;
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  6. #6
    SitePoint Evangelist ucahg's Avatar
    Join Date
    Apr 2001
    Location
    Sarnia, Ontario, Canada
    Posts
    434
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Anarchos, I'm not sure if I understood you correctly, but it didn't work.

    Freddy, that works, thanks!
    If I might ask, what did you do? You enclosed the whole things in brackets, and escaped the closing square brackets.

    I think the escaped closing brackets are just better code, but how does putting the whole thing in brackets help?

    It does work, I just want to understand why (so I learn something )

  7. #7
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    [ and ] are reserved characters in a regex and must be escaped. Also the way you had it it would only replace the content in between the [LI] and [/LI] but it didn't actually replace the whole thing, which is what you wanted Right? So your methdo left behind the [LI] tags it ddidn't swap them out.
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  8. #8
    SitePoint Evangelist ucahg's Avatar
    Join Date
    Apr 2001
    Location
    Sarnia, Ontario, Canada
    Posts
    434
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    They were replaced the original way (except only the last [/LI] tag was parsed as </LI>).

  9. #9
    SitePoint Evangelist ucahg's Avatar
    Join Date
    Apr 2001
    Location
    Sarnia, Ontario, Canada
    Posts
    434
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Okay I tried to add the following code.
    PHP Code:
    <?PHP
    $output 
    eregi_replace("(\[LI=([[:alnum:]])\]([^/LI]*)\[/LI\])","<LI type=\"\\2\">\\3</LI>",$output);
    $output eregi_replace("(\[OL=([[:alnum:]])\](.*)\[/OL\])","<OL type=\"\\2\">\\3</OL>",$output);
    $output eregi_replace("(\[UL=([[:alnum:]])\](.*)\[/UL\])","<UL type=\"\\2\">\\3</UL>",$output);
    ?>
    Which was of course, to add a type attribute to be able to make certain lists different types (square,disc,circle,1,i,I,a,A, for example). But it doesn't work.

    Ideally I'd like it so lists start with squares/numbers and lists nested inside it with discs/roman numerals and lists nested inside that with circles/letters (for example). I'm not sure if that would be possible (and I don't have the skills to create that on my own), so this is my alternative. But it doesn't work correctly.

    With the following code:
    [OL]
    [LI]test[/LI]
    [LI]test[/LI]
    [/OL]

    [UL]
    [LI]test[/LI]
    [LI]test[/LI]
    [/UL]

    [OL]
    [LI]test[/LI]
    [LI=v]test[/LI]
    [LI=V]test[/LI]
    [/OL]

    [UL]
    [LI]test[/LI]
    [LI=square]test[/LI]
    [LI]test[/LI]
    [/Ul]
    But the output returns as:
    [OL=i]<LI>test</LI><LI type="v">test</LI><LI type="V">test</LI></OL>


    [UL=disc]<LI>test</LI>[LI=square]test[/LI]<LI>test</LI></UL>
    Which obviously isn't what I want.
    It'd be great if you could help me


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
  •