expression help needed

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

    #1

    expression help needed

    Hi there. I want to be able to add an extra space in each instance
    after a period in a string. I am already stripping out extra
    whitespace throughout, so to start I have a nice flat string to work
    with. I am struggling getting the syntax right and keep getting funny
    results. What is the simplest way to accomplish this? Thanks a
    billion.

    Matt

  • Brent Palmer

    #2
    Re: expression help needed


    "Matt MC" <mattsimc@yahoo .com> wrote in message
    news:1111437695 .400657.28000@l 41g2000cwc.goog legroups.com...[color=blue]
    > Hi there. I want to be able to add an extra space in each instance
    > after a period in a string. I am already stripping out extra
    > whitespace throughout, so to start I have a nice flat string to work
    > with. I am struggling getting the syntax right and keep getting funny
    > results. What is the simplest way to accomplish this? Thanks a
    > billion.
    >
    > Matt[/color]

    Please give code example.

    Brent Palmer.


    Comment

    • Peter Albertsson

      #3
      Re: expression help needed

      If I understand you correctly, I think this is what you want:

      <?php
      $str = trim(str_replac e('.', '. ', $str));
      ?>

      The trim removes any trailing spaces, if your string ends with a '.'.

      Best Regards,

      Peter

      "Matt MC" <mattsimc@yahoo .com> wrote in message
      news:1111437695 .400657.28000@l 41g2000cwc.goog legroups.com...[color=blue]
      > Hi there. I want to be able to add an extra space in each instance
      > after a period in a string. I am already stripping out extra
      > whitespace throughout, so to start I have a nice flat string to work
      > with. I am struggling getting the syntax right and keep getting funny
      > results. What is the simplest way to accomplish this? Thanks a
      > billion.
      >
      > Matt
      >[/color]


      Comment

      • Matt MC

        #4
        Re: expression help needed

        Greetings. Here are a couple examples of what I am looking for:

        This is the input sentence. Testing this out. Thank you.

        should output to...

        This is the input sentence. Testing this out. Thank you.

        After thinking about this further, I want to do this with all major end
        punctuations, so that would be ! ? and . Did I miss anything?

        Sincerely,

        Matt

        Peter Albertsson wrote:[color=blue]
        > If I understand you correctly, I think this is what you want:
        >
        > <?php
        > $str = trim(str_replac e('.', '. ', $str));
        > ?>
        >
        > The trim removes any trailing spaces, if your string ends with a '.'.
        >
        > Best Regards,
        >
        > Peter
        >
        > "Matt MC" <mattsimc@yahoo .com> wrote in message
        > news:1111437695 .400657.28000@l 41g2000cwc.goog legroups.com...[color=green]
        > > Hi there. I want to be able to add an extra space in each instance
        > > after a period in a string. I am already stripping out extra
        > > whitespace throughout, so to start I have a nice flat string to[/color][/color]
        work[color=blue][color=green]
        > > with. I am struggling getting the syntax right and keep getting[/color][/color]
        funny[color=blue][color=green]
        > > results. What is the simplest way to accomplish this? Thanks a
        > > billion.
        > >
        > > Matt
        > >[/color][/color]

        Comment

        • Geoff Berrow

          #5
          Re: expression help needed

          I noticed that Message-ID:
          <1111450852.859 826.135740@o13g 2000cwo.googleg roups.com> from Matt MC
          contained the following:
          [color=blue]
          >This is the input sentence. Testing this out. Thank you.
          >
          >should output to...
          >
          >This is the input sentence. Testing this out. Thank you.
          >
          >After thinking about this further, I want to do this with all major end
          >punctuations , so that would be ! ? and . Did I miss anything?[/color]

          Is this for output to a browser?

          Because browsers will ignore multiple spaces unless you make them non
          breaking. Also you only want to do the replacement if the character is
          already followed by a space. In this case, using trim will prevent the
          replacement of the final full stop (period).

          <?php

          $punctuation=ar ray(". ","! ","? ");
          $punctuation_wi th_spaces=array (".&nbsp; ","!&nbsp; ","?&nbsp; ");
          $test_string="\ "Can you do it?\" he asked. I thought about it for a
          moment. This would not be as simple as it first seemed. Difficult even!
          Were all options covered? Hard to say. ";
          $str = str_replace($pu nctuation, $punctuation_wi th_spaces,
          $test_string);

          print "<p>$test_strin g</p>";
          print "<p>$str</p>";
          ?>
          --
          Geoff Berrow (put thecat out to email)
          It's only Usenet, no one dies.
          My opinions, not the committee's, mine.
          Simple RFDs http://www.ckdog.co.uk/rfdmaker/

          Comment

          • Joe Webster

            #6
            Re: expression help needed

            Matt MC wrote:[color=blue]
            > Hi there. I want to be able to add an extra space in each instance
            > after a period in a string. I am already stripping out extra
            > whitespace throughout, so to start I have a nice flat string to work
            > with. I am struggling getting the syntax right and keep getting funny
            > results. What is the simplest way to accomplish this? Thanks a
            > billion.
            >
            > Matt
            >[/color]

            This will make sure there are exactly 2 spaces after each of those marks:

            $string = preg_replace("/([\.!?])(\s*)/", "\\1 ", $string);

            -Joe

            Comment

            • ZeldorBlat

              #7
              Re: expression help needed

              str_replace can take an array as the search terms. If any of the
              search terms are found, they will be replaced. So:

              $inputStr = "This is the input sentence. Testing this out. Thank you.";
              $matches = array(". ", "? ", "! "); //one space after each one
              $replacements = array(". ", "? ", "! "); //two spaces after each one
              $outStr = str_replace($ma tches, $replacements, $inputStr);

              Comment

              • Matt MC

                #8
                Re: expression help needed

                Joe, I found if someone types multiple marks, like: testing!!! it will
                output: testing! ! !

                Can this be compensated for those scenarios?

                Sincerely,

                Matt

                Joe Webster wrote:[color=blue]
                > Matt MC wrote:[color=green]
                > > Hi there. I want to be able to add an extra space in each instance
                > > after a period in a string. I am already stripping out extra
                > > whitespace throughout, so to start I have a nice flat string to[/color][/color]
                work[color=blue][color=green]
                > > with. I am struggling getting the syntax right and keep getting[/color][/color]
                funny[color=blue][color=green]
                > > results. What is the simplest way to accomplish this? Thanks a
                > > billion.
                > >
                > > Matt
                > >[/color]
                >
                > This will make sure there are exactly 2 spaces after each of those[/color]
                marks:[color=blue]
                >
                > $string = preg_replace("/([\.!?])(\s*)/", "\\1 ", $string);
                >
                > -Joe[/color]

                Comment

                • Joe Webster

                  #9
                  Re: expression help needed

                  Matt MC wrote:[color=blue]
                  > Joe, I found if someone types multiple marks, like: testing!!! it will
                  > output: testing! ! !
                  >
                  > Can this be compensated for those scenarios?
                  >
                  > Sincerely,
                  >
                  > Matt
                  >
                  > Joe Webster wrote:
                  >[/color]
                  Oh sure, just add a + after the [\.!?], so it should now read.

                  $string = preg_replace("/([\.!?]+)(\s*)/", "\\1 ", $string);

                  -Joe

                  Comment

                  • Matt MC

                    #10
                    Re: expression help needed


                    Joe Webster wrote:[color=blue]
                    > Matt MC wrote:[color=green]
                    > > Joe, I found if someone types multiple marks, like: testing!!! it[/color][/color]
                    will[color=blue][color=green]
                    > > output: testing! ! !
                    > >
                    > > Can this be compensated for those scenarios?
                    > >
                    > > Sincerely,
                    > >
                    > > Matt
                    > >
                    > > Joe Webster wrote:
                    > >[/color]
                    > Oh sure, just add a + after the [\.!?], so it should now read.
                    >
                    > $string = preg_replace("/([\.!?]+)(\s*)/", "\\1 ", $string);
                    >
                    > -Joe[/color]

                    Ahh, that works beautifully. Bravo!

                    ---Matt

                    Comment

                    Working...