user8695752
user8695752

Reputation:

Word search within a sentence and delete previous ones

For example, this sentence:

"Hello Humanity D. Humanity is a wonderful thing"

"Today weather, good D. maybe tomorrow will be bad"

"I could not find another sentence D. this last example sentence"

I have 495 sentences for like this. Here are the sentence to be deleted

"Hello Humanity D. "

"Today weather, good D. "

"I could not find another sentence D. "

The common feature of each letter is the presence of the letter "D."

How do I remove sentences before D.?

function removeEverythingBefore($in, $before) {
    $pos = strpos($in, $before);
    return $pos !== FALSE
        ? substr($in, $pos + strlen($before), strlen($in))
        : "";
}
echo(removeEverythingBefore("I could not find another sentence D. this last example sentence", "D. "));

Image

Upvotes: 2

Views: 72

Answers (5)

user8461611
user8461611

Reputation:

Another suggestion:

$str = 'Hello Humanity D. Humanity is a wonderful thing';
$a = explode("D.",$str);
array_shift($a);
$result = trim(implode("", $a));

This way you won't have to fiddle around with regex. Speed comparison: http://sandbox.onlinephpfunctions.com/code/a078d7ca01c149aabaaddb335a4b0ad8669fb273

Upvotes: 0

JParkinson1991
JParkinson1991

Reputation: 1286

A quick one line way of doing this:

<?php

echo trim(str_replace(' D.', '', strstr($string, ' D.')));

An example with output:

<?php

$string = 'I could not find another sentence D. this last example sentence';

echo trim(str_replace(' D.', '', strstr($string, ' D.')));
//Outputs: this last example sentence

Upvotes: 1

delboy1978uk
delboy1978uk

Reputation: 12365

Try this regex: https://regex101.com/r/mvoJDs/1/

<?php

$strings = [
'Hello Humanity D. Humanity is a wonderful thing',
'Today weather, good D. maybe tomorrow will be bad',    
'I could not find another sentence D. this last example sentence',    
'I have 495 sentences for like this. Here are the sentence to be deleted',   
'Hello Humanity D. ',   
'Today weather, good D. ',    
'I could not find another sentence D.     '
];

$regex =  '#.+\sD\.\s#';

foreach ($strings as $key => $val) {
    $strings[$key] = preg_replace($regex, '', $val);
}

var_dump($strings);

Which will output:

array(7) { 
[0]=> string(29) "Humanity is a wonderful thing" 
[1]=> string(26) "maybe tomorrow will be bad" 
[2]=> string(26) "this last example sentence" 
[3]=> string(71) "I have 495 sentences for like this. Here are the sentence to be deleted" 
[4]=> string(0) "" 
[5]=> string(0) "" 
[6]=> string(4) " " 
}

See it working here https://3v4l.org/kfPnm

Upvotes: 0

David &#214;hlund
David &#214;hlund

Reputation: 64

You can find the position of 'D.' with strpos and substract whats after.

$new_sentance = substr($sentance, strpos($sentance, 'D.') + 1);

Upvotes: 0

Unamata Sanatarai
Unamata Sanatarai

Reputation: 6637

with a preg_replace:

$re = '/.*D\.\s*(.*)/';
$str = 'Hello Humanity D. Humanity is a wonderful thing';
$subst = '$1';

$result = preg_replace($re, $subst, $str);

echo $result;

result: Humanity is a wonderful thing

Upvotes: 1

Related Questions