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;ÃÂ*ÃÂ*ÃÂ*ÃÂ*}}