Solved

PHP/REGEX: Remove last unfinished sentence

Posted on 2014-02-19
4
440 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
[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
  • 3
4 Comments
 
LVL 35

Expert Comment

by:Terry Woods
ID: 39871424
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
ID: 39871432
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
ID: 39871447
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 110

Expert Comment

by:Ray Paseur
ID: 39871508
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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

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…
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…
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…
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.

623 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