A
A
Anton Shelestov2018-10-01 19:45:58
PHP
Anton Shelestov, 2018-10-01 19:45:58

Finding stop words in a sentence using a pattern?

Hello!
Please tell me a regular expression that looks for certain words in the text.
Here's what was done:

$str = 'в начало элементов elements будет добавлен content, который может быть задан html-текстом, объектом jQuery или DOM объектом';

$pattern = ['elements', 'DOM'];

$matches = array();
$matchFound = preg_match_all(
            "/\b(" . implode($pattern,"|") . ")\b/iu",
            $str,
            $matches
 );
var_dump($matches[0])

This code works great, finds everything you need.
But how can you make it so that you can search for words by a pattern, for example, by the word "elem * in", i.e. in this example, the word "elements" will fit this pattern.
Has anyone solved a similar problem?
Thank you!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
0
0xD34F, 2018-10-01
@ShelestovAnt

search for words by a pattern, for example, by the word "elem * in", i.e. in this example, this pattern matches the word "elements"

Well, add the line to $pattern 'элем.*в'.

X
xmoonlight, 2018-10-01
@xmoonlight

for example, by the word "elem * in", i.e. in this example, this pattern matches the word "elements"
Also suitable and "elements elements will be added", because * is any number of any characters.
There is another option.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question