Description
Basically trim_text is used to trim the given text at right and left side of the base string. Using ltrim,rtrim and trim methods are trim only charlist not trimming absolute string or word in PHP. Below example function trim_text() is trimming absolute word at left and right side of the string.
|
function basic_trim($data_str){
$data_str = trim($data_str,' ');
$data_str = trim($data_str,'\r\n');
return $data_str;
}
function trim_word($data_str,$trim_text){
//trimming text right side by given text
$data_str = basic_trim($data_str);
while(is_numeric($pos = strrpos($data_str,$trim_text))){
if ((strlen($data_str) - $pos) == strlen($trim_text))
$data_str = substr($data_str,0,$pos);
else
break;
$data_str = basic_trim($data_str);
}
//trimming text left side by given text
$data_str = basic_trim($data_str);
while(is_numeric($pos = strpos($data_str,$trim_text))){
if ($pos == 0)
$data_str = substr($data_str,$pos+strlen($trim_text),strlen($data_str));
else
break;
$data_str = basic_trim($data_str);
}
return $data_str;
}
|
<?php
$data = "<br /><br /><br /><br />wel<br />come<br /><br />";
$trim_by = "<br />";
echo html_entities(trim_word($data,$trim_by));
?>
|