Google News
logo
Use simplexml_load_string() to read XML data from a string in PHP
`simplexml_load_string()` is a PHP function that allows you to read XML data from a string and convert it into an object that you can manipulate using PHP. Here's an example of how you can use this function :
Program :
$xml = '<fruits>
            <fruit>
                <name>Apple</name>
                <color>Red</color>
            </fruit>
            <fruit>
                <name>Banana</name>
                <color>Yellow</color>
            </fruit>
        </fruits>';

$fruits = simplexml_load_string($xml);

// Accessing the object properties
foreach ($fruits as $fruit) {
    echo "Name: " . $fruit->name . ", Color: " . $fruit->color . "<br>";
}
In this example, we have a string containing XML data representing a list of fruits. We pass this string to the `simplexml_load_string()` function to create an object `$fruits` that we can use to access the individual properties of each fruit.

We then use a `foreach` loop to iterate over each fruit object and print out its name and color properties. Note that we can access the object properties using the arrow operator (`->`).