Как я могу preg_match несколько слов

1

Я пытаюсь сделать ввод не шаблоном.

$input= "words trying to compare";

preg_match("/\b{$input}\b/", "these are a lot of words that i'm trying to compare") 
              // ^this might contain multiple words, but the same as ^^ these
echo "match is found";

как я могу сделать так, что даже если входные слова не находятся в том же порядке, что и предложение, к которому я пытаюсь его сравнить, он может вернуть "соответствие найдено".

Теги:

2 ответа

1
Лучший ответ

Просто используйте preg_match_all

preg_match_all("/\b{$input}\b/", "these are a lot of words that i'm trying to compare",$matchedWords);

Для нескольких слов:

<?php
$input= "words compare";
$pattern = str_replace(" ","|",$input);
preg_match_all("/{$pattern}/", "these are a lot of words that i'm trying to compare words",$matchedWords);
              // ^this might contain multiple words, but the same as ^^ these
var_dump($matchedWords);
  • 0
    Я только что попробовал, и когда ввод «сравнить слова» (поиск в неправильном порядке), он ничего не возвращает.
  • 0
    см обновленный ответ
Показать ещё 2 комментария
0
$input= "words|trying|to|compare";
$count = preg_match_all('#\b('.$input.')\b#', $string, $matches);

if($count)
 echo 'found word';
else
 echo 'not found';

Ещё вопросы

Сообщество Overcoder
Наверх
Меню