Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

PHP/REGEX: Remove last unfinished sentence

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

These days socially coordinated efforts have turned into a critical requirement for enterprises.
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
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…

715 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