Jump to content


Photo

Performance problem


  • Please log in to reply
3 replies to this topic

#1 btherl

btherl
  • Staff Alumni
  • Advanced Member
  • 3,893 posts
  • LocationAustralia

Posted 15 May 2006 - 07:13 AM

I have written a function which acts like explode(), but honours quotes around elements. Here it is:

##############################################################################
# The PHP explode() function, but honouring and stripping double quotes.
# Also recognizes backslash escaped double quotes.
# '"hello, there",fred' => array("hello, there", "fred")
function explode_quote($sep, $instr) {
  $out = array();
  $i = 0;
  $inquotes = false;
  $cur = "";

  while (($c = $instr{$i}) != "") {
    if ($c == '"') {
      $inquotes = !$inquotes;
    } elseif ($c == $sep) {
      if ($inquotes) {
        $cur .= $c;
      } else {
        $out[] = $cur;
        $cur = "";
      }
    } elseif ($c == '\\') { /* Single backslash */
      $i++;
      $cur .= $instr{$i};
    } else {
      $cur .= $c;
    }
    $i++;
  }

  $out[] = $cur;

  return $out;
}

However, it is not very fast. Is there a way I can write this which will make it faster?

#2 redbullmarky

redbullmarky
  • Staff Alumni
  • Advanced Member
  • 2,863 posts
  • LocationBedfordshire, England

Posted 15 May 2006 - 10:05 AM

using regular expressions, you could probably do it in just one line. have a look at preg_split: [a href=\"http://uk2.php.net/preg_split\" target=\"_blank\"]http://uk2.php.net/preg_split[/a]

cheers
Mark
"you have to keep pissing in the wind to learn how to keep your shoes dry..."

I say old chap, that is rather amusing!

#3 btherl

btherl
  • Staff Alumni
  • Advanced Member
  • 3,893 posts
  • LocationAustralia

Posted 17 May 2006 - 01:52 PM

I don't think preg_split() is useful. I need to split on commas, but commas can also be present within the fields, enclosed by quotes, for example:

"first, second",third

This is two comma seperated values:

first,second
third

I can't think of any regexp that will do that for me!

Also, it needs to handle backslash-escaped double quotes within values.

#4 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 17 May 2006 - 02:16 PM

If you use stripslashes() on the string before you start, you won't have to worry about escaped characters. Then your problem is reduced to one problem.

One solution you might want to look at:

<?php
function explode_quote($sep, $instr) {
    $str = trim(stripslashes($instr));
    $tmpfname = tempnam("/tmp", "FOO");
    $fp = f open($tmpfname, "w");
    f write($fp, $str . "\n");
    f close($fp);
    $csvfp = f open($tmpfname,'r');
    $data = f getcsv($csvfp,strlen($str)+10,$sep);
    f close($csvfp);
    unlink($tmpfname);
    return($data);
}?>

The key to this is to use the [a href=\"http://www.php.net/fgetcsv\" target=\"_blank\"]fgetcsv()[/a] function. I don't know if the overhead of creating a temporary file is better than looping through the string character by character.

Note: remove the space between the "f" and the rest of the filesystem functions.

Ken





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users