Search code examples
phpregexstrpos

Finding and replacing all words that ends with 'ing'


I'm trying to find and replace all words that ends with 'ing'. How would I do that?

$text = "dreaming";
if (strlen($text) >= 6) {

if (0 === strpos($text, "ing")) 
//replace the last 3 characters of $text <---not sure how to do this either
echo $text; 
echo "true";    
}

Result:

null

Want Result:

dream
true

Solution

  • You could also use substr

    $text = "dreaming";
    
    if (substr($text, (strlen($text) - 3), 3) === 'ing') {
      $text = substr($text, 0, (strlen($text) - 3));
    }
    echo $text;