Solved

PHP/REGEX: Remove last unfinished sentence

Posted on 2014-02-19
4
436 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 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 108

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
object oriented programming comparison 5 55
WordPress TK Title 8 25
Regex - Ignoring Spaces 7 14
updating the date data 12 25
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…
This article discusses four methods for overlaying images in a container on a web page
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

895 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

12 Experts available now in Live!

Get 1:1 Help Now