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

Recommended Answers

All 4 Replies

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

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;

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; 
        unset($achternaamArray[$key]);    
    }
}

// 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);

Thanks a lot for your help that's exactly what i need.
Again thanks

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.