Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

PHP/REGEX: Remove last unfinished sentence

Posted on 2014-02-19
4
Medium Priority
?
447 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
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 2000 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 111

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

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

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

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
We are witnesses that everyone is saying that our children shouldn't "play" with a technology because it is dangerous. This article is going to prove that they are wrong.
The viewer will learn how to count occurrences of each item in an array.
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…
Suggested Courses

916 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