Texan78 Posted April 9, 2014 Share Posted April 9, 2014 Hello, I have a very simple problem I am sure for some. I have a file I am parsing using SimpleXML. The data includes extra periods after the sentences like this. ...FIRE WEATHER WATCH REMAINS IN EFFECT FROM WEDNESDAY AFTERNOON THROUGH WEDNESDAY EVENING FOR STRONG WINDS AND LOW RELATIVE HUMIDITY FOR THE SOUTHERN AND EASTERN TEXAS PANHANDLE AND THE EASTERN HALF OF THE OKLAHOMA PANHANDLE... * AFFECTED AREA...IN OKLAHOMA...TEXAS AND BEAVER. IN TEXAS... HANSFORD...OCHILTREE...LIPSCOMB...HUTCHINSON...ROBERTS... This is causing me problems with doing preg_replace and strtoupper to capitalize the first letter of the sentence I think due to the extra periods after sentences since it is looking for only one period and not three. My question is how could I go about stripping two of the periods from the three? -Thanks Quote Link to comment https://forums.phpfreaks.com/topic/287625-removing-extra-periods-after-sentance/ Share on other sites More sharing options...
Texan78 Posted April 9, 2014 Author Share Posted April 9, 2014 I got it sorted like this. // Replaces all triple periods with single periods $summary = str_replace('...', '.', $summary); //now capitalize every letter after a . ? and ! followed by space $Summary = preg_replace_callback('/([.!?])\s*(\w)/', function ($matches) { return strtoupper($matches[1] . ' ' . $matches[2]); }, ucfirst(strtolower($summary))); But is there a way to remove periods from certain areas instead of all? Quote Link to comment https://forums.phpfreaks.com/topic/287625-removing-extra-periods-after-sentance/#findComment-1475451 Share on other sites More sharing options...
requinix Posted April 9, 2014 Share Posted April 9, 2014 Probably. What "certain areas"? Quote Link to comment https://forums.phpfreaks.com/topic/287625-removing-extra-periods-after-sentance/#findComment-1475452 Share on other sites More sharing options...
Texan78 Posted April 9, 2014 Author Share Posted April 9, 2014 At the beginning. The data I am parsing adds ... at the beginning of the as well as other areas. The other areas are fine as I was able to remove the extra periods but, at the beginning of the paragraph there is a period that I would like to remove if posible but not certain how to go about that. Quote Link to comment https://forums.phpfreaks.com/topic/287625-removing-extra-periods-after-sentance/#findComment-1475518 Share on other sites More sharing options...
Ch0cu3r Posted April 9, 2014 Share Posted April 9, 2014 Use Trim Quote Link to comment https://forums.phpfreaks.com/topic/287625-removing-extra-periods-after-sentance/#findComment-1475524 Share on other sites More sharing options...
Solution Psycho Posted April 9, 2014 Solution Share Posted April 9, 2014 (edited) $summary = trim(str_replace('...', '.', $summary), '.') . '.'; Trims periods from beginning and end of string then adds a period to the end Edited April 9, 2014 by Psycho Quote Link to comment https://forums.phpfreaks.com/topic/287625-removing-extra-periods-after-sentance/#findComment-1475539 Share on other sites More sharing options...
Texan78 Posted April 9, 2014 Author Share Posted April 9, 2014 Ah trim, I completely forgot about that one. That is exactly what I was looking for and works a lot better. Thanks guys! Quote Link to comment https://forums.phpfreaks.com/topic/287625-removing-extra-periods-after-sentance/#findComment-1475544 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.