Bold, italics, underline str_replac'd using the same char for opening and closing tags

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jean-Fran?ois Lacrampe

    #1

    Bold, italics, underline str_replac'd using the same char for opening and closing tags

    Hello,

    I want to write a _very_ simple text parser that would replace a
    string like:

    "This is text with /italics/, *bold* and _underline_."

    and generate automatically something like this:

    "This is text with <i>italics</i>, <b>bold</b> and <span
    class="underlin e">underline </span>."

    I've found a lot of code snippets explaining how to do BBCode, but
    BBCode has different opening and closing pseudo-tags. In my example,
    the opening and closing tags are the same (either /, * or _). So I
    need something slightly more intelligent that would count the number
    of /, * or _ tags, see if that number is even or odd and decide
    accordingly if it must replace the char with an opening or closing
    tag. (I hope my wording makes sense. It's very clear in my head :-D)

    How would you implement that? I reckon it can't be hard, but I just
    can't figure how to do it right now!

    Thanks for any insight!
  • Janwillem Borleffs

    #2
    Re: Bold, italics, underline str_replac'd using the same char for opening and closing tags

    Jean-Fran?ois Lacrampe wrote:[color=blue]
    > Hello,
    >
    > I want to write a _very_ simple text parser that would replace a
    > string like:
    >
    > "This is text with /italics/, *bold* and _underline_."
    >
    > and generate automatically something like this:
    >
    > "This is text with <i>italics</i>, <b>bold</b> and <span
    > class="underlin e">underline </span>."
    >[/color]

    <?php
    $str = "This is text with /italics/, *bold* and _underline_.";
    $reg = array(
    "!/(.+)/!U",
    "/\*(.+)\*/U",
    "/_(.+)_/U"
    );
    $replace = array(
    "<i>$1</i>",
    "<b>$1</b>",
    "<u>$1</u>"
    );
    $str = preg_replace($r eg, $replace, $str);
    print $str;
    ?>


    JW



    Comment

    • Jean-Fran?ois Lacrampe

      #3
      Re: Bold, italics, underline str_replac'd using the same char for opening and closing tags

      "Janwillem Borleffs" <jw@jwscripts.c om> wrote in message news:<426b9375$ 0$55105$dbd4100 1@news.euronet. nl>...
      [color=blue]
      > <?php
      > $str = "This is text with /italics/, *bold* and _underline_.";
      > $reg = array(
      > "!/(.+)/!U",
      > "/\*(.+)\*/U",
      > "/_(.+)_/U"
      > );
      > $replace = array(
      > "<i>$1</i>",
      > "<b>$1</b>",
      > "<u>$1</u>"
      > );
      > $str = preg_replace($r eg, $replace, $str);
      > print $str;
      > ?>
      >
      >
      > JW[/color]

      WOW those perl regex are powerful! Every now and then I tell me I
      should dig deeper and really understand them (beyond the basic stuff I
      already know). This is pure and deep beauty! Now I want to learn them
      NOW!

      (If you (or anyone else) have and URL for a good tutorial on them,
      please share, it will be greatly appreciated!)

      Thanks, thanks and thanks again.
      JFLac

      Comment

      • Janwillem Borleffs

        #4
        Re: Bold, italics, underline str_replac'd using the same char for opening and closing tags

        Jean-Fran?ois Lacrampe wrote:[color=blue]
        > (If you (or anyone else) have and URL for a good tutorial on them,
        > please share, it will be greatly appreciated!)
        >[/color]

        A good starting point would be:



        Don't forget to read the user contributions on this page, which include a
        couple of useful links.


        JW



        Comment

        Working...