Title: AtomParser::ns_to_prefix
Published: April 25, 2014
Last modified: May 20, 2026

---

# AtomParser::ns_to_prefix( $qname,  $attr = false )

## In this article

 * [Source](https://developer.wordpress.org/reference/classes/atomparser/ns_to_prefix/?output_format=md#source)
 * [Related](https://developer.wordpress.org/reference/classes/atomparser/ns_to_prefix/?output_format=md#related)

[ Back to top](https://developer.wordpress.org/reference/classes/atomparser/ns_to_prefix/?output_format=md#wp--skip-link--target)

## 󠀁[Source](https://developer.wordpress.org/reference/classes/atomparser/ns_to_prefix/?output_format=md#source)󠁿

    ```php
    function ns_to_prefix($qname, $attr=false) {
        # split 'http://www.w3.org/1999/xhtml:div' into ('http','//www.w3.org/1999/xhtml','div')
        $components = explode(":", $qname);

        # grab the last one (e.g 'div')
        $name = array_pop($components);

        if(!empty($components)) {
            # re-join back the namespace component
            $ns = join(":",$components);
            foreach($this->ns_contexts as $context) {
                foreach($context as $mapping) {
                    if($mapping[1] == $ns && strlen($mapping[0]) > 0) {
                        return array($mapping, "$mapping[0]:$name");
                    }
                }
            }
        }

        if($attr) {
            return array(null, $name);
        } else {
            foreach($this->ns_contexts as $context) {
                foreach($context as $mapping) {
                    if(strlen($mapping[0]) == 0) {
                        return array($mapping, $name);
                    }
                }
            }
        }
    }
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-includes/atomlib.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/7.0/src/wp-includes/atomlib.php#L354)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/7.0/src/wp-includes/atomlib.php#L354-L384)

## 󠀁[Related](https://developer.wordpress.org/reference/classes/atomparser/ns_to_prefix/?output_format=md#related)󠁿

| Used by | Description | 
| [AtomParser::end_element()](https://developer.wordpress.org/reference/classes/atomparser/end_element/)`wp-includes/atomlib.php` |  | 
| [AtomParser::start_element()](https://developer.wordpress.org/reference/classes/atomparser/start_element/)`wp-includes/atomlib.php` |  |

## User Contributed Notes

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fclasses%2Fatomparser%2Fns_to_prefix%2F)
before being able to contribute a note or feedback.