Tag Archive for substr

Shorten text without braking words

/*
returns text limited by a specified length of characters but keeping words intact. the final character count will not be exact since it is affected by the possible removing of the a long word or by the addition of the ellipsis.

paramaters:
string - the input string
chars - the length of characters wanted
elli - the ellipsis to be used, defaults to '...'
*/

function shorten($string='', $chars=20, $elli='...'){
list($new_string, $elli)= explode("
", wordwrap($string, $chars, "
", false));
return  ( $elli ) ? $new_string.'...' : $new_string;
}

source

PHP 文字を切り出す

$substr_test1= "日本語です";
$substr_test2 = "English";
$substr_test3 = "日本語English";

//バイト数を指定して文字列を取り出す。
print substr( $substr_test1, 2, 2 ) . "
"; //本
print substr( $substr_test2, 2, 2 ) . "
"; //gl
print substr( $substr_test3, 8 ) . "
";    //glish

//バイト数を指定して文字列を取り出す(右側から)。
print substr( $substr_test1, -4, 2 ) . "
"; //で
print substr( $substr_test2, -4, 2 ) . "
"; //li
print substr( $substr_test3, -4 ) . "
";    //lish

//文字数を指定して文字列を取り出す。
print mb_substr( $substr_test1, 2, 2, 'UTF-8' ) . "
"; //語で
print mb_substr( $substr_test2, 2, 2, 'UTF-8' ) . "
"; //gl
print mb_substr( $substr_test3, 8, 'UTF-8' ) . "
";    //sh

//文字数を指定して文字列を取り出す(右側から)。
print mb_substr( $substr_test1, -4, 2, 'UTF-8' ) . "
"; //本語
print mb_substr( $substr_test2, -4, 2, 'UTF-8' ) . "
"; //li
print mb_substr( $substr_test3, -4, 3, 'UTF-8' ) . "
";    //lish

source

Limit string with/without relating words and from start/end

/*
Description: Function to limit the text with or without relating words and from the start or end of the string
Usage:
$string = txtlmt($string, "30", "...", "1");

Variables:
$txt = string to delimited
$txt_limiter = max amount of letters in the string that should be seen
$txt_sep = the separator used to end the string (like string...)
$tipo
1 - from start to end without relating words (when it reaches the $txt_limiter it breaks the string)
2 - from start to end relating words (when it reaches the $txt_limiter it finds the next space to break the string)
3 - from end to start without relating words (when it reaches the $txt_limiter it breaks the string)
4 - from edn to start relating words (when it reaches the $txt_limiter it finds the next space to break the string)
*/

function txtlmt ($txt,$txt_limiter,$txt_sep,$tipo=0) {

if($tipo=="" OR $tipo==NULL OR $tipo==0) { $tipo = "2"; }

if (strlen($txt) > $txt_limiter) {
// From start to end
if($tipo=="1") { // Do not related words
$txt = substr(put_accents($txt), 0, $txt_limiter) . $txt_sep;
} elseif($tipo=="2") { // Relate words
$txt = substr(put_accents($txt), 0, $txt_limiter);
$txt = substr($txt, 0, strrpos($txt, " ")) . $txt_sep;

// From end to start
} elseif($tipo=="3") { // Do not related words
$txt = $txt_sep.substr(put_accents($txt), -$txt_limiter);
} elseif($tipo=="4") { // Relate words
$txt = substr(put_accents($txt), -$txt_limiter);
$txt = $txt_sep.substr($txt, strpos($txt, " ")+1);
}

$txt = strip_accents($txt);
return $txt;

} else { return $txt; }

}

// #################################################################

// Function to convert strings to HTML characters
function strip_accents ($string) {
$string = ereg_replace("(á)","á",$string);
$string = ereg_replace("(à)","à",$string);
$string = ereg_replace("(ã)","ã",$string);
$string = ereg_replace("(ó)","ó",$string);
$string = ereg_replace("(õ)","õ",$string);
$string = ereg_replace("(é)","é",$string);
$string = ereg_replace("(ú)","ú",$string);
$string = ereg_replace("(í)","í",$string);
$string = ereg_replace("(ç)","ç",$string);
$string = ereg_replace("(Á)","Á",$string);
$string = ereg_replace("(À)","À",$string);
$string = ereg_replace("(Ã)","Ã",$string);
$string = ereg_replace("(Ó)","Ó",$string);
$string = ereg_replace("(Õ)","Õ",$string);
$string = ereg_replace("(É)","É",$string);
$string = ereg_replace("(Ú)","Ú",$string);
$string = ereg_replace("(Í)","Í",$string);
$string = ereg_replace("(Ç)","Ç",$string);
$string = ereg_replace("(")",""",$string);
$string = ereg_replace("(<)","<",$string);
$string = ereg_replace("(>)",">",$string);
$string = ereg_replace("(`)","'",$string);
$string = ereg_replace("(')","'",$string);
$string = ereg_replace("º","º",$string);
$string = ereg_replace("ª","ª",$string);
return $string;
}

// #################################################################

// Function to convert strings back to HTML characters
function put_accents ($string) {
$string = ereg_replace("(á)","á",$string);
$string = ereg_replace("(à)","à",$string);
$string = ereg_replace("(ã)","ã",$string);
$string = ereg_replace("(ó)","ó",$string);
$string = ereg_replace("(õ)","õ",$string);
$string = ereg_replace("(é)","é",$string);
$string = ereg_replace("(ú)","ú",$string);
$string = ereg_replace("(í)","í",$string);
$string = ereg_replace("(ç)","ç",$string);
$string = ereg_replace("(Á)","Á",$string);
$string = ereg_replace("(À)","À",$string);
$string = ereg_replace("(Ã)","Ã",$string);
$string = ereg_replace("(Ó)","Ó",$string);
$string = ereg_replace("(Õ)","Õ",$string);
$string = ereg_replace("(É)","É",$string);
$string = ereg_replace("(Ú)","Ú",$string);
$string = ereg_replace("(Í)","Í",$string);
$string = ereg_replace("(Ç)","Ç",$string);
$string = ereg_replace("(")",""",$string);
$string = ereg_replace("(<)","<",$string);
$string = ereg_replace("(>)",">",$string);
$string = ereg_replace("(')","'",$string);
$string = ereg_replace("º","º",$string);
$string = ereg_replace("ª","ª",$string);
return $string;
}

// #################################################################

source

strMiddleReduceWordSensitive

function strMiddleReduceWordSensitive ($string, $max = 50, $rep = '[...]') {
$strlen = strlen($string);

if ($strlen <= $max)
return $string;

$lengthtokeep = $max - strlen($rep);
$start = 0;
$end = 0;

if (($lengthtokeep % 2) == 0) {
$start = $lengthtokeep / 2;
$end = $start;
} else {
$start = intval($lengthtokeep / 2);
$end = $start + 1;
}

$i = $start;
$tmp_string = $string;
while ($i < $strlen) {
if ($tmp_string[$i] == ' ') {
$tmp_string = substr($tmp_string, 0, $i) . $rep;
$return = $tmp_string;
}
$i++;
}

$i = $end;
$tmp_string = strrev ($string);
while ($i < $strlen) {
if ($tmp_string[$i] == ' ') {
$tmp_string = substr($tmp_string, 0, $i);
$return .= strrev ($tmp_string);
}
$i++;
}
return $return;
return substr($string, 0, $start) . $rep . substr($string, - $end);
}

source

Sub Sentence

if (!function_exists('subsent')) {

function subsent($string, $start = 0, $length = 0, $cap = '...') {

if ($length <= 0 || strlen($string) < $length) { return $string; }

$string = substr($string, $start, strpos($string, ' ', $length)) . ' ' . $cap;

return $string;

}

}

source