Web Hosting Talk







View Full Version : php mysql tokenizing question


dbryant
06-27-2004, 03:36 PM
I want to tokenize a string in PHP that contains quoted strings that should be kept togethe. For example the original string is...

programming php "hello world"

I want the following result...

[1] programming
[2] php
[3] hello world

If I use strtok(), split(), or explode() I get

[1] programming
[2] php
[3] "hello
[4] world"

I could write code to look at each character checking for blanks and quote marks, replacing the former with something unique and removing the quotes, then use strtok() or the like, but I thought that maybe there was a built-in PhP function that works like the C command-line parser (or Google) which treats search string like this.

Thanks. I posted this to a forum yesterday, but can't find it and have got no replies.

Salathe
06-30-2004, 09:29 PM
You can use PHP's strtok (http://www.php.net/strtok) function, albeit a little different than you must have been using it previously.

Example:
<?php

/**
* Using 'strtok' in order to catch words and phrases
* from a string with phrases encapsulated within
* quotation marks.
*/

// Just for debugging, output as plain text
Header("Content-Type: text/plain");

// Our string to tokenize
$string = "This\tis \"an example\" string";

// Token Array
$token = array();

// Catch spaces and tabs
$t = trim(strtok($string, " \t"));

// Looping
while ($t) {
// add to token array
array_push($token, $t);
// move onto next token
$t = trim(strtok("\""));
}

// Debug
print_r($token);
?>

Output:
Array
(
[0] => This
[1] => is
[2] => an example
[3] => string
)

overulenet
07-21-2004, 12:12 PM
yeah,sound a good idea