Portal Home > Knowledgebase > Articles Database > [PHP] Delimiter


[PHP] Delimiter




Posted by FrozenWire, 11-17-2008, 12:10 PM
PHP] Delimiter I have an array with info formatted like: title="value" I am trying to get the data from each item between the "". How do I use quotes as a delimiter in PHP? Anytime I put it into a split or explode function (tried split("\"") and split('"') it fails to see it. Any ideas?

Posted by Ogg, 11-17-2008, 12:44 PM
Is your data actually in an array or is it a String? split() can only work on Strings. Alternatively split('"', $variable) may work but I'm not sure how you're going to do this with your example. Best of luck.

Posted by FrozenWire, 11-17-2008, 01:02 PM
The data in each element of the array is a string. Each element in the array is a line from a large text file and other delimiters work fine. For example split(" ") works fine. Just not the ability to split by ". $array[0] has the value of: title="value" location="value" size="value" What I want to do it walk through and break up that line and put that data into another array so: $newarray[title] => value $newarray[location] => value $newarray[size] => value

Posted by davidma22, 11-17-2008, 01:07 PM
Is each item like so... title="value"? Or do you have keys and values like so... "title" => "value" ? In which case you can use a foreach and insert the values into another array, or into their own variables. PHP Code: foreach($array AS $key => $value){ÂÂ*ÂÂ*ÂÂ*$$key = $value;} That'd create a variable for each key in the array (each "title") and insert the value for that key into that array. Hope that helps.

Posted by 111111z, 11-17-2008, 01:35 PM
single quotes around the double quote in split should work. $str='word"another"andanother'; $array=split('"',$str); print_r($array); __________________Build1UServer.com - Step by step on how to build your own 1U rackmount server on a budget.

Posted by FrozenWire, 11-17-2008, 02:32 PM
Maybe there is a better way to go about my problem. I pull data and it gives me a text file every 10 minutes. lines 1,10,20,30,40,50, etc are the lines I needed and are formatted like: What I did was I walked the text file when it downloads and put the lines I need, (1,10,20,etc) into an array so now each element in the array has each line. I want to now make another array with each original element and just take off the =" " so the new array key is the name and the value is the value between the "'s.

Posted by etogre, 11-17-2008, 03:20 PM
You will need to break it up with a regex in this case. Here's a good regex generator that will show you the code example of how to break it apart in PHP when you are finished. __________________

Posted by bigfan, 11-17-2008, 05:25 PM
Or something like this should work: PHP Code: foreach ($lines as $key => $str) {ÂÂ*ÂÂ*ÂÂ*$str = str_replace(array(''), '', $str);ÂÂ*ÂÂ*ÂÂ*ÂÂ*$arr = explode('" ', trim($str));ÂÂ*ÂÂ*ÂÂ*ÂÂ*foreach ($arr as ÂÂ*ÂÂ*ÂÂ*ÂÂ*}ÂÂ*ÂÂ*ÂÂ*ÂÂ*$newarray[] = $arr;} Or, if it's an xml file: PHP Code: $xobj = simplexml_load_file($file);foreach ($xobj->xpath('//link') as $key => $link_obj) {ÂÂ*ÂÂ*ÂÂ*ÂÂ*foreach ($link_obj->attributes() as $name => $value) {ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*$newarray[$key][$name] = (string) $value;ÂÂ*ÂÂ*ÂÂ*ÂÂ*}}

Posted by Adam-AEC, 11-17-2008, 06:07 PM
PHP Code: '; ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*ÂÂ*print_r(extract_to_array($line)); ?>

Posted by bigfan, 11-17-2008, 06:43 PM
Effective, but (like most regex) slow. 100 lines, typical times: regex method: 0.00603008270264 secs. string function method: 0.00294995307922 secs.

Posted by FrozenWire, 11-17-2008, 06:53 PM
Thanks for all the responses. I will post back with the method that works the best in my case!

Posted by bigfan, 11-17-2008, 07:09 PM
Better str_* function: PHP Code: function get_attributes($str) {ÂÂ*ÂÂ*ÂÂ*$str = str_replace(array('



Was this answer helpful?

Add to Favourites Add to Favourites    Print this Article Print this Article

Also Read