PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, November 20, 2022

[FIXED] How to convert input string to desired(given) format using regex or arrays?

 November 20, 2022     php, preg-match, preg-replace, regex     No comments   

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

https://3v4l.org/NjIai

<?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

Regex 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 “|” |
    • 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) *
    • 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) *
  • 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 “|” |

+($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)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing