0

My input is $text = '( ( LED AND DIODE ) OR ( "LEE power" and system ) ) '

I am applying explode function on this input

function multiexplode ($delimiters,$string) 
{
    $ready = str_replace($delimiters, $delimiters[0], $string);
    $launch = explode($delimiters[0], $ready);
    return  $launch;
}               
$exploded = multiexplode(array(' ',":" ), $text);
for($i=0;$i<(count($exploded));$i++)
    echo "<br> $exploded[$i]";

My output is coming like this

0 - (   
1 - (  
2 - LED  
3 - AND  
4 - DIODE  
5 - )  
6 - OR  
7 - (  
8 - "LEE  
9 - power"  
10 - and  
11 - system  
12 - )  
13 - )  

But I want to output like the following

0 - (  
1 - (  
2 - LED  
3 - AND  
4 - DIODE  
5 - )  
6 - OR  
7 - (  
8 - LEE power  
9 - and  
10 - system  
11 - )  
12 - )  

I want to store all words inside " " in one element of array.

3 Answers3

2

Check the following code:

$text = '( ( LED AND DIODE ) OR ( "LEE power" and system ) ) ';

preg_match_all('/"(?:\.|[^"])*"|\S+/', $text, $exploded);
for($i=0;$i<(count($exploded[0]));$i++)
    echo "<br>". $exploded[0][$i];

UPDATE based on comment

For input of $text = '((LED AND DIODE) OR ("LEE power" and system)) ';

$text = '((LED AND DIODE) OR ("LEE power" and system)) ';
$re = '/"(?:\.|[^"])*"|\(?|\)|(\'(.*?)\'|(\w+))/';
preg_match_all($re, $text, $exploded);

for($i=0;$i<(count($exploded[0]));$i++)
    if($exploded[0][$i]!='')
        echo "<br>". $exploded[0][$i];
Zico
  • 2,349
  • 2
  • 22
  • 25
  • note: when the bracket is touching the " the regex fails: fe.`$text = '( ( "LED AND DIODE" ) OR ("LEE power" and system ) ) '; – Ivo P Jul 06 '17 at 08:21
  • If i remove the space between brackets how can i split the brackets as $text = '((LED AND DIODE) OR ("LEE power" and system)) '; then how can i get same OUTPUT for this can you help me for this ? Thanks in advance – Nasir Ansari Jul 06 '17 at 08:30
  • @NasirAnsari why do you remove brackets? – Zico Jul 06 '17 at 08:37
  • He is talking about the space between the ( and the quote. – Ivo P Jul 06 '17 at 08:43
  • Sorry. My mistake. why do you remove spaces beside brackets? @NasirAnsari – Zico Jul 06 '17 at 08:45
  • I am making a database search software so for making more user friendly i want to remove the spaces – Nasir Ansari Jul 06 '17 at 08:54
  • space between the ( – Nasir Ansari Jul 06 '17 at 08:55
  • I have one question which is different from this, there is a one database search software name is Patseer you can do google search, my question is that i want to know which database system Patseer is using for storing data as Mysql,oracle, Ms-Access etc and which database language Patseer using as SQL etc. Can u help me for that Please. – Nasir Ansari Jul 06 '17 at 12:23
  • @NasirAnsari post different question rather ask in comment! And yet you don't mark any correct answer though your question already solved here and already given a requested answer! – Zico Jul 06 '17 at 18:33
  • If i am storing value in a variable by for loop it is storing array[0], array[1] etc how can i store proper value in a variable by for loop as instead of array[0] store (, array[1] store ( etc . – Nasir Ansari Jul 07 '17 at 09:52
  • in your answer why we are using dot ( . ) before $exploded[0][$i] when we are printing the value by for loop echo "
    ". $exploded[0][$i];
    – Nasir Ansari Jul 07 '17 at 10:30
  • I have solved this But i have another problem which is that in this input $text = '((LED AND DIODE) OR ("LEE power" and system)) '; when i receive output for this input, i am getting "LEE power" with double quote but i want to output of this string without double quote plzzzzz help – Nasir Ansari Jul 07 '17 at 11:18
  • Dot(.) to join string as your answer printed like that @NasirAnsari – Zico Jul 07 '17 at 16:50
  • Its not necessary – Zico Jul 07 '17 at 16:50
  • I have solved this But i have another problem which is that in this input $text = '((LED AND DIODE) OR ("LEE power" and system)) ' when i receive output for this input, i am getting "LEE power" with double quote but i want to output of this string without double quote plzzzzz help – Nasir Ansari Jul 10 '17 at 04:44
  • see my question please – Nasir Ansari Jul 10 '17 at 04:44
1

While this isn't a thoroughly-tested answer, this answers your current issue as you described it. Best of luck!

function concatenateQuotedElements($input_array)
{
  //As per the question, we'll be exploding our array by spaces " ".
  $pieces = explode(" ", $input_array);

  //This array will be returned as our final array of concatenated elements.
  $output_array = array();


  /*When we happen upon an element containing a 
  parenthesis ('"') as the first character, we'll 
  temporarily store so that we may concatenate 
  future elements with it.*/
  $stored_element = null;
  $stored_position = null; 

  /*Parse through our array and look for any character 
  that contains a " and see whether it's at the beginning 
  or the end of the string element.*/
  for ($i = 0; $i < count($pieces); $i++)
  {   

    //If we detect a parenthesis in our element...  
    if (strpos($pieces[$i], '"') !== false)
    { 

      //See if it's at the beginning, and if it is, store it or future use.      
      if (substr($pieces[$i], 0, 1) === '"')
      {
        $stored_element = $pieces[$i];
        $stored_position = $i;
      }

      /*Or, see if it's at the end of the string. If it is, we need to see 
       if there's been a previous element with an opening parenthesis that 
       we've stored and concatenate this element onto our stored element.*/
      if (
          substr($pieces[$i], -1) === '"' && 
          $stored_element !== null && 
          $stored_position !== null
         )
      {
        $output_array[$stored_position] = $stored_element . " " . $pieces[$i];

        /*Reset our temporary storage so that it hold 
        any forthcoming elements with parenthesis.*/  
        $stored_element = null;
        $stored_position = null;

        /*Finally, [Continue] to the next element without 
        pushing this as a non-concatenated, standalone element.*/
        continue;
      }
    }

    array_push($output_array, $pieces[$i]);
  }

  return $output_array;
}

For a specific output pertaining to your given array, you can use:

//Our example array given in the question:
$array = '( ( LED AND DIODE ) OR ( "LEE power" and system ) )';

//Our output:
$concatenate_quotes_array = concatenateQuotedElements($array);

//"Pretty print" the array result of the function
echo "<pre>";
print_r($concatenate_quotes_array);
echo "</pre>";

Output:

Array
(
    [0] => (
    [1] => (
    [2] => LED
    [3] => AND
    [4] => DIODE
    [5] => )
    [6] => OR
    [7] => (
    [8] => "LEE power"
    [9] => and
    [10] => system
    [11] => )
    [12] => )
)
halfer
  • 19,824
  • 17
  • 99
  • 186
Jonathan LeRoux
  • 413
  • 3
  • 12
0

here

<?php
    $text = str_replace('LEE power','LEE-power',"( ( LED AND DIODE ) OR ( 'LEE power' and system ) ) ");
    $newArray = explode(' ',$text);
    foreach($newArray as $row) $data[] = str_replace('-',' ',$row);
    echo "<pre>";print_r($data);die;
?>
GYaN
  • 2,327
  • 4
  • 19
  • 39
  • 1
    instead of 'LEE power' in string it can be any words then how can i do ? – Nasir Ansari Jul 06 '17 at 07:13
  • if you know that word or where it appear, just replace spaces between them by `-`; and after explode again replace `-` by space. – GYaN Jul 06 '17 at 07:15
  • I am creating database search software so this whole string is giving by user mean user can search any think then how can i manage this ? – Nasir Ansari Jul 06 '17 at 07:18