Solved

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

Posted on 2014-02-19
2
445 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 35

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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

Whatever be the reason, if you are working on web development side,  you will need day-today validation codes like email validation, date validation , IP address validation, phone validation on any of the edit page or say at the time of registration…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

621 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