I have data between tags
<ml><locale name="en-US">125LKO.50C-SL137</locale>
<locale name="es-ES"></locale>
</ml>
like wise,I want to data into array in like this format
arr['en']= "125LKO.50C-SL137"
arr['es']= "-";
or
arr['0']= "125LKO.50C-SL137"
arr['1']= "-";
I am using this function
function get_string_between($string){
$string = preg_replace('/<[^>]*>/', "~", $string);
$words = explode('~', $string);
return array_values(array_filter($words));
}
How may i get data into array in php, Please help me.
You can use regex which may not be the best solution but it would go something like:
<locale.*>(.*)<\/locale>
Test it with regex101 for instance, it works. Use preg_match_all like this:
// $string is your DATA to search in
$string = '<ml><locale name="en-US">125LKO.50C-SL137</locale>
<locale name="es-ES"></locale>
</ml>';
preg_match_all("/<locale.*>(.*)<\/locale>/", $string , $matches); // use preg_match only if searching for first occurrence only
foreach($matches[0] as $match){
echo $match; // first one is en and second is es
}
Dont forget: parsing xml/html with regex is usually not a good idea. Try to use dedicated parser instead.
In PHP, you can do this:
$rule = '/\<locale name\=\"([\w\.\-\_]*)\"\>([\w\.\-\_]*)\<\/locale\>/';
And then you do:
$array = array();
preg_match_all($ruke, $string, $array);
You could use PHP: XmlReader to read elements of your Xml file. A regular expression is ok and you can receive what you want with it, but if you want to be more flexible I' prefer the PHP Xml Reader class
Alright, I'll bite. Parsing XML with regex is like trying to cut down a tree with a hammer. Sure, you might eventually end up with something that looks like it works, but you'll spend too much time writing code that handles those cases where the pattern you use just doesn't quite cut it.
Markup languages, like XML or HTML are best parsed. And Luckily, PHP comes with a couple of tools that do just that: Parse markup. Just off the bat, here's how I'd process the data you have:
$dom = new DOMDocument;
$dom->loadXML($xml);
$xpath = new DOMXpath($dom);
$nodes = $xpath->query('ml/locale');
$result = array();
foreach ($nodes as $node)
{
$result[] = array(
'val' => $node->nodeValue,
'nameAttr' => $node->attributes->getNamedItem('name')->value
);
}
var_dump($result);
As you can see here, it works just fine. Of course, this code isn't copy-paste ready at all, but it should be enough to get you started. Look into xpaths, and the DOMDocument
API. It's worth the time, promise!
Just to help you a bit more, in reality, you'd check the return value of $node->attributes->getNamedItem
first, before accessing a property on its return value, as it can return null
:
foreach ($nodes as $node)
{
$attribute = $node->attributes->getNamedItem('name') ? : null;
$result[] = array(
'val' => $node->nodeValue,//empty string, or node contents/value
'name' => $attribute ? $attribute->value : null//null, or the name attribute value
);
}
That would be a more reliable approach.
You could try the below PHP code,
<?php
$mystring = <<< 'EOT'
<ml><locale name="en-US">125LKO.50C-SL137</locale>
<locale name="es-ES"></locale>
</ml>
EOT;
$regex = '~(?m)<ml><locale\s*name="..\K-|(?<=>).+?(?=<)~';
preg_match_all($regex, $mystring, $matches);
var_dump($matches);
?>
Output:
array(1) {
[0]=>
array(2) {
[0]=>
string(1) "-"
[1]=>
string(16) "125LKO.50C-SL137"
}
}