Created
December 3, 2010 14:15
-
-
Save DaveChild/726996 to your computer and use it in GitHub Desktop.
PHP SoftHyphens Function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
This function will add soft hyphens after every 3rd character in words of over 10 characters. | |
*/ | |
public static function SoftHyphens($text) { | |
$return = ''; | |
// Add spaces around HTML tag delimiters, to process them as individual words (to be removed later) | |
$text = str_replace('>', '> ', $text); | |
$text = str_replace('<', ' <', $text); | |
// Split text into words using whitespace as a delimiter, also capturing delimiters | |
$words = preg_split('/(\s)/m', $text, 0, PREG_SPLIT_DELIM_CAPTURE); | |
// Loop through all words | |
for ($j = 0, $wordCount = count($words); $j < $wordCount; $j++) { | |
// Split word into letters (this method added to work around issue with multi-byte characters) | |
$letters = preg_split('/(?<!^)(?!$)/u', $words[$j]); | |
$wordLength = count($letters); | |
// If words is an HTML tag or attribute, skip it. Attributes are hard-coded but could be dynamic. | |
if ( | |
(strpos($words[$j], '<') === false) | |
&& (strpos($words[$j], '>') === false) | |
&& (strpos($words[$j], 'href="') === false) | |
&& (strpos($words[$j], 'target="') === false) | |
&& ($wordLength > 10) | |
) { | |
$inside_character = false; | |
for ($i = 0; $i < $wordLength; $i++) { | |
$return .= $letters[$i]; | |
// Don't add shy hyphens inside HTML-encoded entities | |
if ($letters[$i] == '&') { | |
$inside_character = true; | |
} | |
if (($inside_character) && ($letters[$i] == ';')) { | |
$inside_character = false; | |
} | |
if (!$inside_character) { | |
// Add a shy hyphen every third letter, unless it's a period or near the end of the word | |
if ((($i % 3) == 2) && ($i < ($wordLength - 5)) && ($i > 3)) { | |
$return .= '­'; | |
if ($letters[$i] == '.') { | |
$delayed_insert = true; | |
} | |
} elseif ($delayed_insert) { | |
// We added one near a period. Add one later, because the previous one will be removed. | |
$return .= '­'; | |
$delayed_insert = false; | |
} | |
} | |
} | |
} else { | |
// Add unaltered word to output. | |
$return .= $words[$j]; | |
} | |
} | |
// Remove soft hyphens that are next to a period | |
$return = str_replace('.­', '.', $return); | |
$return = str_replace('­.', '.', $return); | |
// Remove the spaces we added earlier | |
$return = str_replace('> ', '>', $return); | |
$return = str_replace(' <', '<', $return); | |
// Return the formatted text | |
return $return; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment