Friday, September 19, 2025
HomeLanguagesPHP | DOMNode lookupPrefix() Function

PHP | DOMNode lookupPrefix() Function

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




<?php
 
// Create a new DOMDocument instance
$document = new DOMDocument();
 
// Load the XML with no namespace
$document->loadXML("<?xml version=\"1.0\"?>
    <div >
        <h1> neveropen </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




<?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;\">
         neveropen
        </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
 

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!
RELATED ARTICLES

Most Popular

Dominic
32299 POSTS0 COMMENTS
Milvus
84 POSTS0 COMMENTS
Nango Kala
6664 POSTS0 COMMENTS
Nicole Veronica
11837 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11895 POSTS0 COMMENTS
Shaida Kate Naidoo
6779 POSTS0 COMMENTS
Ted Musemwa
7054 POSTS0 COMMENTS
Thapelo Manthata
6738 POSTS0 COMMENTS
Umr Jansen
6744 POSTS0 COMMENTS