PHP 限制字符串有/无关联单词和开始/结束
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PHP 限制字符串有/无关联单词和开始/结束相关的知识,希望对你有一定的参考价值。
/*
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;
}
// #################################################################
以上是关于PHP 限制字符串有/无关联单词和开始/结束的主要内容,如果未能解决你的问题,请参考以下文章