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...

You mean like

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

Or am I am misunderstanding what you want?

@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 - )

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.

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 

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?

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...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.