Friday, September 18, 2015

Readmore function using explode function

<?php

$string = "Lorem ipsum dolor sit amet, consectetur adipisicing elit. Dolorum in minima necessitatibus, neque odit optio perspiciatis quasi ratione suscipit voluptates! Alias blanditiis corporis distinctio exercitationem, facere ipsa necessitatibus quo saepe.";

function read_more($string, $length = 20){
   $string_array = explode(' ', $string);
    $string_array_length = count($string_array);
    $new_string = "";
    if($length > $string_array_length){
        $length = $string_array_length;
    }
   for($i = 0; $i < $length; $i++){
     $new_string .= $string_array[$i] . ' ';
   }
    return $new_string;
}
echo read_more($string, 200);

No comments:

Post a Comment

css snippet for blogger code highlighting

code, .code {     display: block;     background: beige;     padding: 10px;     margin: 8px 15px; }