Solved

PHP/REGEX: Remove last unfinished sentence were period ends sentence

Posted on 2014-02-19
2
428 Views
Last Modified: 2014-02-19
Using PHP and REGEX, how can I remove the last incomplete sentence?   For example,
Hello, world.  Joe D. is a
Should become:
Hello, world.

This code works if there are no initials followed by periods:
$mystring = preg_replace("/[\w\s',]*$/",'',$mystring);

Open in new window

http://www.experts-exchange.com/Programming/Languages/Regular_Expressions/Q_28369053.html

Any period that follows one single letter does not end a sentence.

Thanks!
0
Comment
Question by:hankknight
2 Comments
 
LVL 35

Accepted Solution

by:
Terry Woods earned 500 total points
ID: 39871629
This will work for the example provided:
$mystring = preg_replace("/([^!.?]|[A-Z]\.)*$/",'',$mystring);

Open in new window


However, when the initial is an upper case letter i then there is a very small chance (depending on your data) that it's not actually an initial and it's part of the sentence instead, as "I" is a word.
0
 
LVL 34

Expert Comment

by:Dan Craciun
ID: 39871662
$mystring = preg_replace("/[\w\s\',]*(( \w\.)*[\w\s\',]*)*$/",'',$mystring);

Open in new window


This works for all the following cases:
Hello, world.  Joe D. is a
Hello, world.  Joe is a
Hello, World. Joe
Hello, world. Joe B. joe d.
Hello, world. b. jor d.

HTH,
Dan
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now