How to get the complete word of the character that is obtained with stripos()? (PHP)

29 Views Asked by At

I can't find the documentation that indicates how to do it. I am dynamically displaying a part of a post description in the search results from my website.

Example:

<?php
$extract = "Include all the information someone would need to answer your question.";
$search = "format";
$num = stripos($extract,$search);

$to_show = substr($extract,$num, 17);

echo $to_show;
?>

Result:

formation someone

I would like to be able to show "information" and not "formation". Any suggestion?

1

There are 1 best solutions below

2
Tim Biegeleisen On BEST ANSWER

Actually regular expressions work nicely for your particular problem. Search for \w*format\w* using preg_match_all:

$extract = "Include all the information someone would need to answer your question.";
preg_match_all("/\w*format\w*/", $extract, $matches);
print_r($matches[0]);

This prints:

Array
(
    [0] => information
)