1,105,384 Community Members

Php slugify two variables together

Member Avatar
everton.retweets
Junior Poster in Training
71 posts since Jan 2013
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Hi Everyone, I am using the following slugify script and would like to concatenate two php variables.

The two variables I would like to concatenate are $firstname & $surname.

The end result should be firstname-surname.

                function slugify($text)
                {
                // replace non letter or digits by -
                    $text = preg_replace('~[^\\pL\d]+~u', '-', $text);
                // trim
                    $text = trim($text, '-');
                // transliterate
                    if (function_exists('iconv'))
                    {
                        $text = iconv('utf-8', 'us-ascii//TRANSLIT', $text);
                    }
                // lowercase
                    $text = strtolower($text);
                // remove unwanted characters
                    $text = preg_replace('~[^-\w]+~', '', $text);
                    if (empty($text))
                    {
                        return 'n-a';
                    }
                    return $text;
                }

Any help would be appreciated...

Member Avatar
pixelsoul
Posting Shark
918 posts since Feb 2011
Reputation Points: 174 [?]
Q&As Helped to Solve: 147 [?]
Skill Endorsements: 25 [?]
 
0
 

You mean like

$fullname = $firstname . " " . $surname;
slugify($fullname);

Or am I am misunderstanding what you want?

Member Avatar
everton.retweets
Junior Poster in Training
71 posts since Jan 2013
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

@pixelsoul - Thanks for replying, This doesnt give me the correct result.

I am trying to get the firstname, surname in the following format
firstname-surname - But when the data is added to the database it is entered as firstname surname (without the - )

Member Avatar
pixelsoul
Posting Shark
918 posts since Feb 2011
Reputation Points: 174 [?]
Q&As Helped to Solve: 147 [?]
Skill Endorsements: 25 [?]
 
0
 

The issue is most likely outside of this function, as the function it self seems fine. Post the rest of the relevant code that you have, and we'll see where the problem is.

Member Avatar
everton.retweets
Junior Poster in Training
71 posts since Jan 2013
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Hi, All I am doing afterwards is adding the variable $fullname to a list of variables that are updating a mysql database.

   function slugify($text)
                {
                // replace non letter or digits by -
                    $text = preg_replace('~[^\\pL\d]+~u', '-', $text);
                // trim
                    $text = trim($text, '-');
                // transliterate
                    if (function_exists('iconv'))
                    {
                        $text = iconv('utf-8', 'us-ascii//TRANSLIT', $text);
                    }
                // lowercase
                    $text = strtolower($text);
                // remove unwanted characters
                    $text = preg_replace('~[^-\w]+~', '', $text);
                    if (empty($text))
                    {
                        return 'n-a';
                    }
                    return $text;
                }

$fullname = $firstname . " " . $surname;
slugify($fullname);

    $firstname      = "$firstname";  //1
    $surname        = "$surname";  //2
    $fullname       = "$fullname"; //3 
Member Avatar
pixelsoul
Posting Shark
918 posts since Feb 2011
Reputation Points: 174 [?]
Q&As Helped to Solve: 147 [?]
Skill Endorsements: 25 [?]
 
0
 

That example I gave was just an example. If you needed to pass the string through the function and then to the DB:

$firstname = "Tom";
$surname = "Brady";

// separate var in case you needed to use the name in this format on the site somewhere.
$fullname = $firstname . " " . $surname;

// set a var with the string you want to pass through the function.
$nameslug = slugify($fullname);

Now you would pass $firstname, $surname, and $nameslug to the DB. The only reason I would create a separate var $nameslug, is if I wanted/needed to use the $fullname var somewhere on the site to show both the firstname and surname separated by a space.

I'm not clear on what you're doing here

$firstname      = "$firstname";  //1
$surname        = "$surname";  //2
$fullname       = "$fullname"; //3

Why is the above needed?

Member Avatar
everton.retweets
Junior Poster in Training
71 posts since Jan 2013
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Yes that has done the trick, the solution was in creating an new variable

$nameslug = slugify($fullname);

Thanks very much for your help...
DaniWeb rules again...

Question Answered as of 3 Months Ago by pixelsoul
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article