Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
PHP SoftHyphens Function
<?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 .= '&#173;';
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 .= '&#173;';
$delayed_insert = false;
}
}
}
} else {
// Add unaltered word to output.
$return .= $words[$j];
}
}
// Remove soft hyphens that are next to a period
$return = str_replace('.&#173;', '.', $return);
$return = str_replace('&#173;.', '.', $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