I am using a cut string function like function cut_string($string_to_cut, $number_words, $append="Read more"){ $string = explode(" ", $string_to_cut); if(count($string) < $number_words){ for($x=0; $x < $number_words; $x++) { $string_return .= $string[$x] . " "; } $return_string = substr($string_return, 0, -1); } else { for($x=0; $x < $number_words; $x++) { $string_return .= $string[$x] . " "; } $string = substr($string_return, 0, -1); $return_string = $string.' <a href="#">'.$append.'</a>'; } return($return_string); } Code (markup): $intro = cut_string($text, 30); But now I need the rest of the string, so from 30 until the end???How can I get the rest in a variable???
tx nikolas for pointing me in the right direction. But I am stuck I have added this to my function $data['intro'] = $return_string; $data['rest'] = substr($string_to_cut, $number_words); function cut_string($string_to_cut, $number_words, $append="Read more") { $string = explode(" ", $string_to_cut); if(count($string) < $number_words){ for($x=0; $x < $number_words; $x++) { $string_return .= $string[$x] . " "; } $return_string = substr($string_return, 0, -1); } else { for($x=0; $x < $number_words; $x++) { $string_return .= $string[$x] . " "; } $string = substr($string_return, 0, -1); $return_string = $string.' <a href="#" >'.$append.'</a>'; } $data['intro'] = $return_string; $data['rest'] = substr($string_to_cut, $number_words); return $data; } Code (markup): But that didn't work as I aspected, where do I go wrong???
As I am in a little rush, I wont read your code. What do you want this to do? Give a memo and get the first n words from it?
function cut_string($string_to_cut, $number_words, $append="Read more") { $string = explode(" ", $string_to_cut); if(count($string) < $number_words){ for($x=0; $x < $number_words; $x++) { $string_return .= $string[$x] . " "; } $return_string = substr($string_return, 0, -1); } else { for($x=0; $x < $number_words; $x++) { $string_return .= $string[$x] . " "; } $string = substr($string_return, 0, -1); $return_string = $string.' <a href="#" >'.$append.'</a>'; } $data['intro'] = $return_string; $data['rest'] = substr($string_to_cut, $number_words); return $data; } $test = 'The use of cookies on this forum is optional, but may enhance your experience of the site.'; $intro = cut_string($test, 3); echo "Introduction: <strong>".$intro['intro']."</strong><br/><br/>"; echo "Rest: ".$intro['rest']; ?> Code (markup): Intro shows "The use of" as expected and I thought the output for Rest: would be cookies on this forum is optional, but may enhance your experience of the site. Code (markup): But it is use of cookies on this forum is optional, but may enhance your experience of the site Code (markup):