The DOMNode::lookupPrefix() function is an inbuilt function in PHP which is used to get the namespace prefix of the node based on the namespace URI.
Syntax:
string DOMNode::lookupPrefix( string $namespaceURI )
Parameters: This function accepts a single parameter $namespaceURI which holds the namespace URI.
Return Value: This function returns the prefix of the namespace.
Below examples illustrate the DOMNode::lookupPrefix() function in PHP:
Example 1:
<?php
// Create a new DOMDocument instance
$document = new DOMDocument();
// Load the XML with no namespace
$document->loadXML("<?xml version=\"1.0\"?>
<div >
<h1> GeeksforGeeks </h1>
</div>
");
// Get the prefix with namespace URI "my_namespace"
$prefix = $document->documentElement->
lookupPrefix("my_nsamespace");
echo $prefix;
?>
Output:
// Empty string as there is no such namespace
Example 2:
<?php
// Create a new DOMDocument instance
$document = new DOMDocument();
// Load the XML with a namespace with prefix x
$document->loadXML("<?xml version=\"1.0\"?>
<div xmlns:x=\"my_namespace\">
<x:h1 x:style=\"color:red;\">
GeeksforGeeks
</x:h1>
</div>
");
// Get the prefix with namespace URI "my_namespace"
$prefix = $document->documentElement->
lookupPrefix('my_namespace');
echo $prefix;
?>
Output:
x
Reference: https://www.php.net/manual/en/domnode.lookupprefix.php