0

I have the following string from a form...

Opera "adds cross-platform hardware" "kicks butt" -hippies

In general I've simply been using the following...

$p0 = explode(' ',$string);

However now I want to maintain any and all quote operators as a single array item instead of having them create individual items like "adds, cross-platform and hardware".

I want to have that string end up creating an array like this...

Array
(
    [0] => 'Opera',
    [1] => 'adds cross-platform hardware',
    [2] => 'kicks butt',
    [3] => '-hippies'
)

I generally prefer to not use regex for most things whenever possible.

1

4 Answers 4

5

You could use a preg_match_all(...):

$text = 'Opera "adds cross-platform hardware" "kicks butt" -hippies';
preg_match_all('/"(?:\\\\.|[^\\\\"])*"|\S+/', $text, $matches);
print_r($matches);
Sign up to request clarification or add additional context in comments.

3 Comments

I forgot about regex...should have stated that I try to avoid regex whenever possible to avoid performance hits. If I can't find an acceptable way to do this and your answer works I'll accept it though since I did not initially state I would prefer a non-regex answer. If you update your answer to include a non-regex way comment and I'll try that out too. Thanks!
@John, a non-regex solution is not necessarily faster. Don't micro-optimize. Code for clarity, wait for a performance problem, then profile and optimize as needed.
@FrancisAvila With server code I care a lot about performance and if I get an answer I want to make sure everyone can not only learn from it though also get the past answer from it. Regex has it's place though like other things in programming some people grow overly dependent on it. I'll go over the answers and try to figure out what works the fastest.
3

If you're using PHP >= 5.3, you can use str_getcsv

print_r(str_getcsv('Opera "adds cross-platform hardware" "kicks butt" -hippies'," "));

prints

Array
(
    [0] => Opera
    [1] => adds cross-platform hardware
    [2] => kicks butt
    [3] => -hippies
)

Comments

0

try this without using regex, a rough code

<?
$string='Opera "adds cross-platform hardware" "kicks butt" -hippies';
$g=explodeMe($string);
echo "<pre>";
print_r($g);
echo "</pre>";

function explodeMe($string){
    $k=explode('"',$string);
    foreach ($k as $key => $link)
    {
        if ($k[$key] == ' ')
        {
            unset($k[$key]);
        }
    }
    return array_values($k);
}
?>

Comments

0

While I'm looking for the fastest approach I thought I would add my own approach to the challange.

<?php
$q = 'Opera "adds cross-platform hardware" "kicks butt" -hippies';
echo '<div>'.$q.'</div>';
$p0 = explode(' ',$q);
echo '<div><pre>';print_r($p0);echo '</pre></div>';

$open = false;
$terms = array();
foreach ($p0 as $key)
{
 if ($open==false)
 {
  if (substr($key,0,1)=='"')
  {
   $open = $key;
  }
  else {array_push($terms,$key);}
 }
 else if (substr($key,strlen($key) - 1,strlen($key))=='"')
 {
  $open = $open.' '.$key;
  array_push($terms,$open);
  $open = false;
 }
 else
 {
  $open = $open.' '.$key;
 }
}

echo '<div><pre>';print_r($terms);echo '</pre></div>';

echo '<div><pre>';print_r($open);echo '</pre></div>';
?>

Outputs the following...

Opera "adds cross-platform hardware" "kicks butt" -hippies

//Initial explode by spaces...

Array (

[0] => Opera
[1] => "adds
[2] => cross-platform
[3] => hardware"
[4] => "kicks
[5] => butt"
[6] => -hippies

)

//Final results...

Array (

[0] => Opera
[1] => "adds cross-platform hardware"
[2] => "kicks butt"
[3] => -hippies

)

2 Comments

Are you using PHP<5.3? Otherwise a built-in functions should be fast enough for you...
No, not (currently) on my live (shared) server.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.