I have a Collada (.dae) file. Within the file there are a lot of tags of this format;
<float_array id="adsfsafdas" count="72">12 4 2 1 92 1</float_array>
I want to extract the string from between all these tags and populate an array, so in this example it’ll save the string “12 4 2 1 92 1” into the array.
I have a function to find strings between two given strings but the “id” and “count” in the opening tag change for each one. From a bit of searching it seems that regular expressions are the way to go to match the pattern and “preg_match” might be of use.
Any tips about how to go about it? Thanks
EDIT: Thanks for the advice – I got it working with the following code!
<?PHP
//open collada file
$file = file_get_contents('samplecollada.dae');
//find all matches and populate array
preg_match_all("/\<float_array id\=\".+\" count\=\".+\"\>(.+)\<\/float_array\>/",$file, $results);
//output array to preview result
print_r($results[1]);
?>
Here‘s a basic reference of regex special characters. Here‘s the documentation on PHP’s
preg_match(). What you want to do is match a literal<float_array, then a variable set of attributes (using something like.*?or[^>]*?, then a literal>, then the numbers (which you’ll need to group so that you can backreference them), then the literal</float_array>. In PHP, you can pass a$matchesvariable to capture backreferences (again, see documentation above). If you use/as your delimiter, you’ll need to escape your slashes within your regex with\. I’m sure you can piece the rest together.