Jump to content

Performance problem


btherl

Recommended Posts

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

[code]##############################################################################
# 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;
}[/code]

However, it is not very fast. Is there a way I can write this which will make it faster?
Link to comment
Share on other sites

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:

[code]"first, second",third[/code]

This is two comma seperated values:

[code]first,second
third[/code]

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

Also, it needs to handle backslash-escaped double quotes within values.
Link to comment
Share on other sites

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:

[code]<?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);
}?>[/code]

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
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.