Skip to Content
Menu

valid_hostname_regex()

Creates a regular expression for valid hostnames.

PHP April 28, 2017

Usage

PHP
nebula()->valid_hostname_regex($domains)

Parameters

$domains
(Optional) (Array) An array of domains
Default: Current domain

Request or provide clarification »

Additional Notes

This can be used for Google Analytics filters.

Was this page helpful? Yes No


    A feedback message is required to submit this form.


    Please check that you have entered a valid email address.

    Enter your email address if you would like a response.

    Thank you for your feedback!

    Source File

    Located in /libs/Utilities/Utilities.php on line 411.

    1 Hook

    Find these filters and actions in the source code below to hook into them. Use do_action() and add_filter() in your functions file or plugin.

    Filters
    "hostname_regex"
    Need a new filter hook? Request one here.

    Actions
    This function has no action hooks available. Request one?

    PHP
            public function valid_hostname_regex($domains=null){
                $domains = ( !empty($domains) && is_array($domains) )? $domains : array($this->url_components('domain')); //If a domain is not passed, use the current domain
    
                //Add hostnames from Nebula Options
                if ( $this->get_option('hostnames') ){
                    $domains = array_merge($domains, explode(',', str_replace(' ', '', esc_html($this->get_option('hostnames')))));
                }
    
                $domains = array_merge($domains, array('googleusercontent.com', 'googleweblight.com')); //Add default safe valid domains to the list
                $all_domains = apply_filters('hostname_regex', $domains); //Allow other functions/plugins to add/remove domains from the array
    
                $all_domains = preg_filter('/^/', '.*', $all_domains); //Add wildcard prefix to all domains
                $all_domains = str_replace(array(' ', '.', '-'), array('', '\.', '\-'), $all_domains); //Final regex should be: \.*gearside\.com|\.*gearsidecreative\.com
                $all_domains = array_unique($all_domains); //De-dupe the domain list
    
                return implode("|", $all_domains); //Return a valid hostname regex string
            }
    

    Override

    This function can not be short-circuited with an override filter. Request one?