Issue
I have input string coming from user like this
" |appl | pinea | orang frui | vege lates"
and I want to convert it to this format
"+(appl* | pinea* | orang*) +(frui* | vege*) +(lates*)"
I have tried with preg_match and arrays but unable to find solution
$term = " |appl | pinea | orang frui | vege lates";
$term = trim($term, "| ");
$term = preg_replace("#[\| ]{2,}#", " | ", $term);
// $term value is "appl | pinea | orang frui | vege lates" after applying safe guards
// $matches = [];
// $matched = preg_match_all("#(\P{Xan}+)\|(\P{Xan}+)#ui", $term, $matches);
// var_dump($matches);
$termArray = explode(" ", $term);
foreach($termArray as $index => $singleWord) {
$termArray[$index] = trim($singleWord);
}
$termArray = array_filter($termArray);
foreach($termArray as $index => $singleWord) {
if($singleWord === "|") {
$pipeIndexes[] = $index;
$orIndexes[] = $index - 1;
$orIndexes[] = $index + 1;
unset($termArray[$index]);
}
}
// desired string I am hoping for
// "+(appl* | pinea* | orang*) +(frui* | vege*) +(lates*)"
desired string I am hoping for "+(appl* | pinea* | orang*) +(frui* | vege*) +(lates*)"
Solution
This assumes you have cleaned up your input string to the following:
appl | pinea | orang frui | vege lates
Quick working example
<?php
$subject = 'appl | pinea | orang frui | vege lates';
// Add the "*" in place at end
$subject = preg_replace('/[a-z]+/i', '$0*', $subject);
// Group the terms adding brackets and + sign
$result = preg_replace('/(?:[^\s|]+\s*\|\s*)*(?:[^\s|]+)/i', '+($0)', $subject);
var_dump($result);
// Outputs:
// string(53) "+(appl* | pinea* | orang*) +(frui* | vege*) +(lates*)"
Add in the *
after each word:
$result = preg_replace('/[a-z]+/i', '$0*', $subject);
Group based on space
$result = preg_replace('/(?:[^\s|]+\s*\|\s*)*(?:[^\s|]+)/i', '+($0)', $subject);
Visualisation
Human Readable
(?:[^\s|]+\s*\|\s*)*(?:[^\s|]+)
Options: Case insensitive; Exact spacing; Dot doesn’t match line breaks; ^$ don’t match at line breaks; Greedy quantifiers; Regex syntax only
- Match the regular expression below
(?:[^\s|]+\s*\|\s*)*
- Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
*
- Match any single character NOT present in the list below
[^\s|]+
- Between one and unlimited times, as many times as possible, giving back as needed (greedy)
+
- A “whitespace character” (any Unicode separator, tab, line feed, carriage return, vertical tab, form feed, next line)
\s
- The literal character “|”
|
- Between one and unlimited times, as many times as possible, giving back as needed (greedy)
- Match a single character that is a “whitespace character” (any Unicode separator, tab, line feed, carriage return, vertical tab, form feed, next line)
\s*
- Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
*
- Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
- Match the character “|” literally
\|
- Match a single character that is a “whitespace character” (any Unicode separator, tab, line feed, carriage return, vertical tab, form feed, next line)
\s*
- Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
*
- Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
- Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
- Match the regular expression below
(?:[^\s|]+)
- Match any single character NOT present in the list below
[^\s|]+
- Between one and unlimited times, as many times as possible, giving back as needed (greedy)
+
- A “whitespace character” (any Unicode separator, tab, line feed, carriage return, vertical tab, form feed, next line)
\s
- The literal character “|”
|
- Between one and unlimited times, as many times as possible, giving back as needed (greedy)
- Match any single character NOT present in the list below
+($0)
- Insert the character “+” literally
+
- Insert an opening parenthesis
(
- Insert the whole regular expression match
$0
- Insert a closing parenthesis
)
Answered By - Dean Taylor Answer Checked By - Dawn Plyler (PHPFixing Volunteer)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.