0

I am using this code to split my content after the first punctuation mark, retrieving the fist sentence.

$content = preg_split('/(?<=[!?.])./', $content);

How can I remove that remaining punctuation mark at the end of the splitted sentence?

webmasters
  • 5,663
  • 14
  • 51
  • 78

5 Answers5

0

you can run after this

$content = ltrim($content, '.');

or

$content = str_replace('.', '', $content);
Haim Evgi
  • 123,187
  • 45
  • 217
  • 223
  • no, if you use ltrim its remove only the dot in the end of the sentence, str_replace replace every occurrence of dot – Haim Evgi Feb 07 '12 at 13:05
  • I need something to remove all punctuation – webmasters Feb 07 '12 at 13:05
  • ok look on http://stackoverflow.com/questions/4762546/php-the-best-way-to-remove-punctuation-marks-symbols-diacritics-special-char or http://stackoverflow.com/questions/4751308/remove-the-punctuation-mark-with-regular-expression – Haim Evgi Feb 07 '12 at 13:08
0

You can use:

$content = substr($content, 0, -1);

This will remove the last character from $content.

maya
  • 67
  • 2
  • 9
0
$content = preg_split('/[!?.]/', $content, null, PREG_SPLIT_NO_EMPTY);
JRL
  • 76,767
  • 18
  • 98
  • 146
0

try dis code!

$test   =   'Ho! My! God!';
    $temp   =   split('!',trim($test,'!'));
    echo "=".__LINE__."=><pre>";print_r($temp);echo "</pre>";
    Array
(
    [0] => Ho
    [1] =>  My
    [2] =>  God
)
K6t
  • 1,821
  • 1
  • 13
  • 21
0

How about:

$content = "Hello, world! What's new today? Everything's OK.";
$arr = preg_split('/[!?.] ?/', $content, -1, PREG_SPLIT_NO_EMPTY);
print_r($arr);

This splits on punctuation mark !?. followed by an optionnal space. The captured strings don't contain leading space.

The difference with yours is that I don't catch the punctuation.

output:

Array
(
    [0] => Hello, world
    [1] => What's new today
    [2] => Everything's OK
)
Toto
  • 89,455
  • 62
  • 89
  • 125