ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Extract First Name from Screen Name

March 28, 2011 9:59pm

Subscribe [2]
  • #1 / Mar 28, 2011 9:59pm

    kirkaracha

    273 posts

    How can I extract a person’s first name from their screen name? (The screen names are the people’s full names.) This isn’t working either in a snippet or in a template set to PHP on Output:

    <?php
    $screen_name = '{screen_name}';
    $name = explode(" ",$screen_name);
    $first_name = $name[0];
    ?>
    Welcome <?php echo $first_name; ?>

    I just get the full screen name; the PHP doesn’t seem to do anything.

    EE 2.1.3/Build 20101220/MSM

  • #2 / Mar 29, 2011 6:24am

    Mark Bowen

    12637 posts

    What is your PHP parsing stage set to for the template which this code is on? Try flipping it and see if that helps.

    Best wishes,

    Mark

  • #3 / Mar 29, 2011 11:45am

    kirkaracha

    273 posts

    Switching PHP to parse on Input doesn’t do anything, it still shows the complete screen name.

    I put the same code in an embedded template and the behavior is the same.

  • #4 / Mar 29, 2011 12:28pm

    Mark Bowen

    12637 posts

    Hmm odd!

    I might be wrong on this but it might be that you need two parsing stages here due to setting the php variable to that of the screen_name and then trying to utilise that variable.

    On a quick test I did it shows that outputting the array using var_dump does indeed show that you just get something like this :

    array(1) {
    [0]=>
    string(23) "EE214 Administrator"
    }
    Welcome EE214 Administrator

    Perhaps a plugin might be the way to go here?

    Best wishes,

    Mark

  • #5 / Mar 29, 2011 2:01pm

    kirkaracha

    273 posts

    I’ll try to do it as a plugin. My first attempt didn’t turn out so good.

    class Get_first_name {
    
        var $return_data = '';
        function __construct(){
            $this->EE =& get_instance(); 
            $screen_name = $this->EE->TMPL->tagdata;
            $name = explode(' ',$screen_name);
            $first_name = $name[0];
            $this->return_data = $first_name;
        }
    
    }

    If I wrap the screen_name in my EE template with {exp:get_first_name} I get a random alphanumeric string. If I wrap the screen_name with {get_first_name} it displays the entire screen_name.

    I’ve used CodeIgniter for several years but I haven’t done any EE add-ons yet so I’m sure I’m missing something.

  • #6 / Mar 29, 2011 2:08pm

    Mark Bowen

    12637 posts

    If you just want to use the {screen_name} of the logged in user then you’d probably be best off using :

    $screen_name = $this->EE->session->userdata['screen_name'];

    to get the screen name for use in your function.

    Hope that helps a bit.

    Best wishes,

    Mark

  • #7 / Mar 29, 2011 4:20pm

    kirkaracha

    273 posts

    class Get_first_name {
    
        var $return_data = '';
        function __construct(){
            $this->EE =& get_instance();
            $screen_name = $this->EE->session->userdata['screen_name']; 
    //        $screen_name = $this->EE->TMPL->tagdata;
            $name = explode(' ',$screen_name);
            $first_name = $name[0];
            $this->return_data = $first_name;
        }
    
    }

    Returns this: M00o93H7pQ09L8X1t49cHY01Z5j4TT91fGfr

    Changing it to return $screen_name instead of $first_name gives the same result.

  • #8 / Mar 29, 2011 6:24pm

    Mark Bowen

    12637 posts

    No promises but give this a go :

    <?php
    
    /*
    =========================================================================
     Copyright (c) 2011 Mark Bowen Design
    =========================================================================
     File: pi.explode_first_name.php V2.0
    -------------------------------------------------------------------------
     Purpose:    Find persons first name by exploding on a space
                 in the {screen_name}
    =========================================================================
    CHANGE LOG :
    
    29th March 2011
        - Version 1.0
        - Creation of initial plugin
    =========================================================================
    */
    
    
    $plugin_info = array(
                            'pi_name'            => 'Explode First Name',
                            'pi_version'        => '1.0',
                            'pi_author'            => 'Mark Bowen',
                            'pi_author_url'        => 'http://www.markbowendesign.com/',
                            'pi_description'    => 'Find persons first name by exploding on a space in the {screen_name}',
                            'pi_usage'            => Explode_first_name::usage()
                        );
    
    
    class Explode_first_name {
    
        var $return_data = "";
    
        function Explode_first_name()
        {
            $this->EE =& get_instance();
            $tagdata = $this->EE->TMPL->tagdata;
            $screen_name = $this->EE->session->userdata['screen_name'];
    
            $parts = explode(" ", $screen_name);
            $first_name = $parts[0];
            $last_name = $parts[1];
    
            foreach ($this->EE->TMPL->var_single as $key => $val)
            {
                    if ($key == "first_name")
                    {
                    $tagdata = $this->EE->TMPL->swap_var_single($key, $first_name, $tagdata);
                    }
                    if ($key == "last_name")
                    {
                    $tagdata = $this->EE->TMPL->swap_var_single($key, $last_name, $tagdata);
                    }
            }
            
            // Spit it out
            $this->return_data = $tagdata;
    
    }
    
    // END
    
    
    
    // ----------------------------------------
    //  Plugin Usage
    // ----------------------------------------
    
    // This function describes how the plugin is used.
    // Make sure and use output buffering
    
    function usage()
    {
    ob_start();
    ?>
    
    A nice easy one this one!
    
    {exp:explode_first_name name="Mark Bowen"}
    {first_name}{last_name}
    {/exp:explode_first_name}
    
    
    <?php
    $buffer = ob_get_contents();
    
    ob_end_clean();
    
    return $buffer;
    }
    // END
    
    
    }
    // END CLASS
    
    /* End of file pi.explode_first_name.php */ 
    /* Location: ./system/expressionengine/third_party/explode_first_name/pi.explode_first_name.php */

    Usage

    {exp:explode_first_name}
    {first_name}, {last_name}
    {/exp:explode_first_name}

    Will need to go into a file called pi.explode_first_name.php in a folder called explode_first_name like this :

    system > expressionengine > third_party > explode_first_name > pi.explode_first_name.php

    Best wishes,

    Mark

  • #9 / Mar 29, 2011 6:32pm

    kirkaracha

    273 posts

    That works great, thanks!

  • #10 / Mar 29, 2011 6:40pm

    Mark Bowen

    12637 posts

    Excellent news. Then my work here is done 😊

    Best wishes,

    Mark

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases