Solved

PHP/REGEX: Remove last unfinished sentence

Posted on 2014-02-19
4
435 Views
Last Modified: 2014-02-19
Using PHP and REGEX, how can I remove the last incomplete sentence?   For example,

This:
Hello world.  This is a

Should become:
Hello world.

However:
This is a test.

should remain unchanged.
0
Comment
Question by:hankknight
  • 3
4 Comments
 
LVL 35

Expert Comment

by:Terry Woods
Comment Utility
If you mean the last incomplete sentence in a string, then this should do it:

$mystring = preg_replace('/[\w\s]*$/','',$mystring);

Open in new window

0
 
LVL 35

Expert Comment

by:Terry Woods
Comment Utility
If you're wanting to work on a per-paragraph basis, it will be more complicated...
0
 
LVL 35

Accepted Solution

by:
Terry Woods earned 500 total points
Comment Utility
This may work a little better, for cases where the text to be removed includes an apostrophe or comma:
$mystring = preg_replace("/[\w\s',]*$/",'',$mystring);

Open in new window

0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
I cannot do the brain today.  I have the dumb.  So instead of a REGEX, I would just do it with simple string manipulation.
http://iconoun.com/demo/temp_hankknight.php

<?php // demo/temp_hankknight.php
error_reporting(E_ALL);

// SEE http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28369053.html

$tests = array
( 'Hello world.  This is a'
, 'This is a test.'
, 'Elipses...'
)
;

// WITH EACH OF THE TEST CASES
foreach ($tests as $test)
{
    echo '<br>' . declop($test);
}

// A FUNCTION TO REMOVE TRAILING SENTENCE FRAGEMENTS
function declop($str, $key = '.')
{
    $str = strrev($str);
    $pos = strpos($str, $key);
    if ($pos) $str = substr($str, $pos, strlen($str));
    return strrev($str);
}

Open in new window

0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
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…

763 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

7 Experts available now in Live!

Get 1:1 Help Now