preg_replace question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Sam

    preg_replace question

    Hi

    Can anyone help me with this problem please?

    I have a long string that I would like to split into paragraphed
    sentences. I would like to replace every full-stop or period ('.')
    with the HTML tags "</p><p>", however, the string also contains
    decimal numbers such as 2.4 and some sentences end with a number. But
    no sentences starts with a number.

    I am guessing I need to use preg_replace() ?

    Thanks in advance.

    Sam
  • Pedro Graca

    #2
    Re: preg_replace question

    Sam wrote:[color=blue]
    > Hi
    >
    > Can anyone help me with this problem please?
    >
    > I have a long string that I would like to split into paragraphed
    > sentences. I would like to replace every full-stop or period ('.')
    > with the HTML tags "</p><p>", however, the string also contains
    > decimal numbers such as 2.4 and some sentences end with a number. But
    > no sentences starts with a number.
    >
    > I am guessing I need to use preg_replace() ?
    >
    > Thanks in advance.
    >
    > Sam[/color]

    Wouldn't it be enough to replace all ". " with ".</p><p>"?

    <?php
    $new_text = preg_replace('@ \.\s@', '.</p><p>', $text);
    ?>


    This way you'll get what you intend with

    - The cat is here. The dog is there.
    - Please see point 4. Or 5, I don't remember.

    The last period in these examples isn't followed by white space and does
    not get replaced with the snippet above. I think that's ok for the end
    of the string or you'd end up with "</p><p></p>" in your HTML.
    --
    --= my mail box only accepts =--
    --= Content-Type: text/plain =--
    --= Size below 10001 bytes =--

    Comment

    • Sam

      #3
      Re: preg_replace question

      Thanks for your reply, but I forgot to mention (sorry) that there are
      no spaces after the periods! Is there a way to match a period in
      between 2 letters and not between 2 numbers and then replace the
      period with '.</p><p>' ?



      [color=blue]
      >
      > Wouldn't it be enough to replace all ". " with ".</p><p>"?
      >
      > <?php
      > $new_text = preg_replace('@ \.\s@', '.</p><p>', $text);
      > ?>
      >
      >[/color]

      Comment

      • John Dunlop

        #4
        Re: preg_replace question

        Sam wrote upsidedown:
        [color=blue]
        > Is there a way to match a period in between 2 letters and not between
        > 2 numbers and then replace the period with '.</p><p>' ?[/color]

        $string = preg_replace(
        '`(?<=[a-z])\.(?=[a-z])`i',
        '.</p><p>',
        $string)

        You'd have to add other characters to the character class if you need
        them.

        So tell me, why are you doing this?

        --
        Jock

        Comment

        • Sam

          #5
          Re: preg_replace question

          My solution:

          function _insertP($str){
          $str[0] = str_replace("." ,".</p><p>",$str[0]);
          return $str[0];
          }

          $text_new = preg_replace_ca llback('@\.[A-Z]@','_insertP',$ text);

          Comment

          Working...