Knowledge Walls
John Peter
Pune, Maharashtra, India
How to trim word in php with Example in Computer software engineer articles of One day One Thing to Know
   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.
Utility Function
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);
       $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));
        $data_str = basic_trim($data_str);
    return $data_str;

$data = "<br /><br /><br /><br />wel<br />come<br /><br />";
$trim_by = "<br />";
echo html_entities(trim_word($data,$trim_by));

wel<br />come
Best Lessons of "One day One Thing to Know"
Top lessons which are viewed more times.
  Copyright © 2014 Knowledge walls, All rights reserved
keep your tutorials and learnings with KnowledgeWalls. Don't lose your learnings hereafter. Save and revise it whenever required.
Click here for more details