Removing certain items in a string:Use a regex or for loop?

Discussion in 'PHP' started by UCDaZ, Jul 3, 2010.

  1. #1
    I want to write a snippet of code that would delete the period at the end of sentences.
    However, periods that precede lowercase letters should NOT be removed.
    Examples of strings that should NOT remove the period
    * Go by 123 Abc St. and C street
    * e.g., here
    * this is a longstate... followed by


    Examples of strings that should have their periods removed
    "That's something." Bob said. He was being... As always. Don't you know?
    Becomes:
    "That's something" Bob said He was being As always Don't you know?

    Is there one regex I could write or should I just use a for-loop and iterate through the strings and keep track of the periods and capitalized letters?

    Any ideas would be greatly appreciated!
    Thanks!
     
    UCDaZ, Jul 3, 2010 IP
  2. sarahk

    sarahk iTamer Staff

    Messages:
    28,901
    Likes Received:
    4,555
    Best Answers:
    123
    Trophy Points:
    665
    #2
    I'd try and work out some standard rules and to a str_replace where ." becomes " and so on. Then finally check the last character, if it's a . then use substr
     
    sarahk, Jul 4, 2010 IP
  3. UCDaZ

    UCDaZ Active Member

    Messages:
    180
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #3
    Thanks for your suggestion!
    But here is a string where I DO NOT remove the period.
    "That's something." like I said

    Notice how the letter is lowercase after the period even though there's a " after the period.

    Anyone else have any other ideas?
     
    UCDaZ, Jul 4, 2010 IP
  4. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #4
    Can you reply with an example input and expected output.
     
    danx10, Jul 4, 2010 IP
  5. UCDaZ

    UCDaZ Active Member

    Messages:
    180
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #5
    Here's an example input and output

    Input:
    "That's something." Bob said. He was being... As always. Don't you know?

    Output:
    "That's something" Bob said He was being As always Don't you know?


    Examples of strings that should NOT remove the period since the letter following the period is not uppercase
    * Go by 123 Abc St. and C street
    * e.g., here
    * this is a longstate... followed by
     
    Last edited: Jul 4, 2010
    UCDaZ, Jul 4, 2010 IP
  6. K1llswitch

    K1llswitch Member

    Messages:
    84
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    28
    #6
    K1llswitch, Jul 4, 2010 IP
  7. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #7
    <?php
    $str = '"That\'s something." Bob said. He was being... As always. Don\'t you know?';
    
    //remove a dot/full-stop if its followed by a capital letter.
    $str = preg_replace('~\. (?=[A-Z]{1})~', ' ', $str);
    
    //remove a dot/full-stop if its followed by "
    $str = str_replace('." ', '" ', $str);
    
    echo $str;
    ?>
    PHP:
     
    Last edited: Jul 4, 2010
    danx10, Jul 4, 2010 IP
  8. UCDaZ

    UCDaZ Active Member

    Messages:
    180
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #8
    Thanks danx10!

    But I notice your preg_replace doesn't replace multiple periods in the same sub string.
    For example:
    He was being... As always
    should be
    He was being As always


    But good job so far!
     
    UCDaZ, Jul 5, 2010 IP
  9. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #9
    <?php
    $str = '"That\'s something." Bob said. He was being... As always. Don\'t you know?';
    
    //remove a dot/full-stop if its followed by a capital letter.
    $str = preg_replace('~[\.]+ (?=[A-Z]{1})~', ' ', $str);
    
    //remove a dot/full-stop if its followed by "
    $str = str_replace('." ', '" ', $str);
    
    echo $str;
    ?>
    PHP:
     
    danx10, Jul 5, 2010 IP
  10. UCDaZ

    UCDaZ Active Member

    Messages:
    180
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #10
    How would I remove the periods before the quote?
    For example:

    Input:
    "That's something..." Bob said. He was being... As always. Don't you know? Like "the..." never "the..." Cat

    Output:
    "That's something" Bob said He was being As always Don't you know? Like "the..." never "the" Cat
     
    UCDaZ, Jul 5, 2010 IP
  11. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #11
    <?php
    $str = '"That\'s something." Bob said. He was being... As always. Don\'t you know?';
    
    //remove a dot/full-stop if its followed by a capital letter.
    $str = preg_replace('~[\.]+ (?=[A-Z]{1})~', ' ', $str);
    
    //remove a dot/full-stop if its followed by "
    $str = preg_replace('~[\.]+" ~', '" ', $str);
    
    echo $str;
    ?>
    PHP:
     
    danx10, Jul 5, 2010 IP
  12. UCDaZ

    UCDaZ Active Member

    Messages:
    180
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #12
    Great! Thanks everyone!
     
    UCDaZ, Jul 5, 2010 IP