So much bad code arises from the fact that people don’t know that there’s a better way. If a developer doesn’t know about “split” and “join” functions, they might write a cumbersome for loop to manipulate a string. If they don’t know about regexes, they might really overcomplicate some string munging . Or heck, maybe they don’t understand a for loop, and use a while instead.

Maurycy submits this PHP code. It’s hard to really understand how this particular version of the code came into being.

$pos=0;
while (substr($contentData->name,$pos,1)!=""){
    $letters[] = substr($contentData->name,$pos,1);
    $pos++;
}
foreach ($letters as $key=>$letter){
    preg_match('/[A-Za-z0-9_]/',$letter,$matches);
    if (empty($matches)){
        $letters[$key]="_";
    }
}
$contentName = implode($letters);