Skip to content
Related Articles

Related Articles

Improve Article

PHP | DOMNode lookupPrefix() Function

  • Last Updated : 02 Mar, 2020

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 ecamples 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




My Personal Notes arrow_drop_up
Recommended Articles
Page :