W3docs

simplexml_load_string()

SimpleXML is a PHP extension that provides a simple and easy-to-use API for working with XML documents. The SimpleXMLElement::loadString() function is one of

Introduction

SimpleXML is a PHP extension that provides a simple and easy-to-use API for working with XML documents. The simplexml_load_string() function is one of the many functions that SimpleXML provides to work with XML documents. It is a powerful tool that can be used to load an XML document from a string and create a SimpleXMLElement object. In this article, we will be discussing the simplexml_load_string() function in detail and how it can be used in PHP.

Understanding the simplexml_load_string() function

The simplexml_load_string() function in PHP loads an XML document from a string and creates a SimpleXMLElement object. (Note: This is the procedural approach; the object-oriented equivalent is new SimpleXMLElement($string).) The syntax for using the simplexml_load_string() function is as follows:

Syntax

simplexml_load_string ( string $data [, string $class_name = "SimpleXMLElement" [, int $options = 0 [, string $ns = "" [, bool $is_prefix = false ]]]] ) : SimpleXMLElement

Here, $data is the string containing the XML document to load. $class_name is an optional parameter that specifies the name of the class to use for the SimpleXMLElement object. $options is an optional parameter that specifies additional options for loading the XML document. $ns is an optional parameter that specifies the namespace to use for the XML document. $is_prefix is an optional parameter that specifies whether the namespace is a prefix.

Example Usage

Let's look at an example to understand the usage of the simplexml_load_string() function in PHP:

Example Usage of the simplexml_load_string() function in PHP

php— editable, runs on the server

In the example above, we first create a string containing an XML document. We then use the simplexml_load_string() function to load the XML document from the string and create a SimpleXMLElement object. We use a foreach loop to iterate over each book element in the XML document and print the title of each book.

If your XML comes from a file or a URL instead of a string, use simplexml_load_file() instead.

Reading attributes

XML attributes are accessed with array-style syntax ($element['attrName']), while child elements use property syntax ($element->child). This is the most common gotcha for newcomers:

<?php

$xmlString = '<books><book id="1"><title>PHP Basics</title></book></books>';
$xml = simplexml_load_string($xmlString);

foreach ($xml->book as $book) {
  echo "ID: " . $book['id'] . " - Title: " . $book->title . "\n";
}

This prints ID: 1 - Title: PHP Basics. Note that attribute values are themselves SimpleXMLElement objects, so cast them with (string) or (int) if you need a plain scalar.

Using the options parameter

The third parameter accepts libxml option constants that change how the document is parsed. A frequently used one is LIBXML_NOCDATA, which merges CDATA sections into plain text nodes so you can read them like ordinary content:

<?php

$xmlString = '<note><body><![CDATA[Hello <b>world</b>]]></body></note>';
$xml = simplexml_load_string($xmlString, "SimpleXMLElement", LIBXML_NOCDATA);

echo (string) $xml->body . "\n";

Without LIBXML_NOCDATA, $xml->body would be empty when accessed as a string because the content lives in a CDATA node.

Error Handling

By default, simplexml_load_string() will emit warnings if the XML is malformed. To handle errors gracefully, you can use libxml_use_internal_errors():

<?php

$xmlString = '<books><book><title>PHP Basics</title></books>'; // Missing closing </book> tag
libxml_use_internal_errors(true);
$xml = simplexml_load_string($xmlString);

if ($xml === false) {
  foreach (libxml_get_errors() as $error) {
    echo "Error: ", trim($error->message), "\n";
  }
} else {
  foreach($xml->book as $book) {
    echo $book->title . "\n";
  }
}

When the XML is invalid, simplexml_load_string() returns false rather than throwing an exception. Calling libxml_use_internal_errors(true) suppresses the default warnings and lets you inspect the collected errors with libxml_get_errors(). Always check for false before using the result.

Conclusion

simplexml_load_string() parses an XML string into a SimpleXMLElement object, giving you object-style access to child elements ($xml->child) and array-style access to attributes ($xml['attr']). Reach for it when the XML is already in memory; use simplexml_load_file() when it lives in a file or URL. For robust code, enable internal libxml errors and check the return value before reading the result. To go deeper into the SimpleXML API, see the SimpleXML overview.

Practice

Practice
What does the 'simplexml_load_string' function in PHP do according to https://www.w3docs.com/learn-php/simplexml-load-string.html?
What does the 'simplexml_load_string' function in PHP do according to https://www.w3docs.com/learn-php/simplexml-load-string.html?
Was this page helpful?