Reputation: 31
Here is my post method input from form. The Form includes search technique.
What are the difference between AC motor and DC motor?
I want the output to be
What|difference|AC|motor|DC|motor
But I am getting the output
What|||difference||AC|motor||DC|motor
What am I doing wrong?
Here is my coding approach
<?php
include 'dbh.inc.php';
if(isset($_POST['submit']) && !empty($_POST['search'])){
$value = trim(mysqli_real_escape_string($conn, $_POST['search']));
$noSpace = preg_replace('/\s+/', ' ', $value);
$noCommon = removeCommonWords($noSpace);
$replace = str_replace(' ', '|', $noCommon);
echo $replace;
}
function removeCommonWords($input){
// EEEEEEK Stop words
$commonWords = array('a','able','about','above','abroad',..........);
return preg_replace('/\b('.implode('|',$commonWords).')\b/','',$input);
}
?>
Upvotes: 2
Views: 82
Reputation: 626689
You may match and skip common words and only match and keep other word chunks:
\b(?:are|the|between|and)\b(*SKIP)(*F)|\w+
See the regex demo.
Details
\b(?:are|the|between|and)\b
- whole words are
, etc.(*SKIP)(*F)
- the PCRE verbs that discard the match and proceed looking for a next one from the end of the unsuccessful match|
- or\w+
- just match and keep 1 or more word chars.Here is a PHP snippet:
$commonWords = ['are','the','between','and'];
$value = 'What are the difference between AC motor and DC motor?';
$rx = '~\b(?:' . implode('|', $commonWords) . ')\b(*SKIP)(*F)|\w+~u';
if (preg_match_all($rx, $value, $matches)) {
echo implode('|', $matches[0]); // => What|difference|AC|motor|DC|motor
}
Upvotes: 1