Hi hope someone can help me with the folowing
i have a string with a few words i want to delete some words before the string and uppercase the first letter from the string that over.

$achternaam = "van der hurk";           
$common_words = array("van", "der", "de");
$achternaam = str_replace($common_words, "", $achternaam);
echo ucfirst( strtolower( $achternaam ) );

if i echo $achternaam then the ucfirst dosn't work.
with that code.
Can someone help me

3 Years
Discussion Span
Last Post by johnnyd1963

The $achternaam variable contains spaces before the actual name and the first space gets capitalized by ucfirst function. Use trim to get rid of the spaces:

echo ucfirst( strtolower( trim($achternaam) ) );

Edited by broj1


thanks that works great.

I have another question

How can i echo the romoved words again ?
Or better i want them to use somewhere else.
something like $removed_words = removed words;

Edited by johnnyd1963


See explanation in comments:

$common_words = array("van", "der", "de");

// initialize an array for removed words
$removedWords = array();

// save string to array
$achternaamArray = explode(' ', $achternaam);

// cycle through the array
foreach($achternaamArray as $key => $namePart) {

    // check if current word is in common words array
    // or is empty string (after trimming)
    if(in_array($namePart, $common_words) || trim($namePart) == '') {

        // if yes, add it to the removed words array
        // and remove it from the main array
        $removedWords[] = $namePart; 

// now you have only unremoved words in the $achternaamArray
// implode it to string (and capitalize first letter)
echo  ucfirst(implode(' ', $achternaamArray)) . '<br>';

// display the removed words
echo 'Removed words: ' . implode(', ', $removedWords);
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.