Cut string and rest of the string ????

Discussion in 'PHP' started by 123GoToAndPlay, Apr 13, 2007.

  1. #1
    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???
     
    123GoToAndPlay, Apr 13, 2007 IP
  2. Nikolas

    Nikolas Well-Known Member

    Messages:
    1,022
    Likes Received:
    22
    Best Answers:
    0
    Trophy Points:
    150
    #2
    Nikolas, Apr 13, 2007 IP
  3. 123GoToAndPlay

    123GoToAndPlay Peon

    Messages:
    669
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #3
    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???
     
    123GoToAndPlay, Apr 13, 2007 IP
  4. Nikolas

    Nikolas Well-Known Member

    Messages:
    1,022
    Likes Received:
    22
    Best Answers:
    0
    Trophy Points:
    150
    #4
    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?
     
    Nikolas, Apr 13, 2007 IP
  5. JochenVandeVelde

    JochenVandeVelde Peon

    Messages:
    412
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Can you post what is going wrong? e.g. the output of tht function using a sample string.
     
    JochenVandeVelde, Apr 13, 2007 IP
  6. 123GoToAndPlay

    123GoToAndPlay Peon

    Messages:
    669
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #6
    
    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):
     
    123GoToAndPlay, Apr 14, 2007 IP