Return to Snippet

Revision: 17538
at September 9, 2009 08:56 by DaveChild


Initial Code
function SoftHyphens($text) {
    $return = '';
    $words = preg_split('/([^A-Za-z0-9]+)/m', $text, 0, PREG_SPLIT_DELIM_CAPTURE);
    for ($j = 0, $wordCount = count($words); $j < $wordCount; $j++) {
        $wordLength = strlen($words[$j]);
        if ($wordLength > 10) {
            for ($i = 0; $i < $wordLength; $i++) {
                $return .= $words[$j][$i];
                if ((($i % 3) == 2) && ($i < ($wordLength - 3)) && ($i > 0)) {
                    $return .= '&#173;';
                }
            }
        } else {
            $return .= $words[$j];
        }
    }
    return $return;
}

Initial URL
http://www.addedbytes.com

Initial Description
This function will add soft hyphens after every 3rd character in words of over 10 characters. It will not leave fewer than three characters following a soft hyphen.

Known bugs: Adds soft hyphens to URLs and within HTML tags.

Initial Title
PHP SoftHyphens Function

Initial Tags
php

Initial Language
PHP