+ Reply to Thread
Page 1 of 2 12 LastLast
Results 1 to 10 of 14

Thread: [PHP] cut off 160 chars

  1. #1
    diabolo's Avatar
    diabolo is offline Community Advocate diabolo is on a distinguished road
    Join Date
    Nov 2007
    Location
    Jersey Shore
    Posts
    1,683

    [PHP] cut off 160 chars

    I want to write up a script to separate a message if it is >160 chars, and if it is separate it to as many as needed.

    this is what I have so far
    PHP Code:
    // < 160 > ?
    $lengthover strlen($message);
    if(
    $lengthover 160){
      
    $messagepart_1 substr($message160);

    but what happens if 160th character is in the middle of a word

  2. #2
    xPlozion's Avatar
    xPlozion is offline x10 Elder xPlozion is an unknown quantity at this point
    Join Date
    Mar 2008
    Location
    Delaware, USA
    Posts
    872

    Re: [PHP] cut off 160 chars

    if it's in the middle of a word, it will split the word. say your last word is 'hello', it will split it at 'he' if that's where the 160th char falls. i've seen various custom functions if it falls within a word, but i forget it, i'll try to look it up for you.

    [EDIT]
    try:
    PHP Code:
    <?php
    // Credit goes to: svihel
    // http://us3.php.net/manual/en/function.substr.php#84103

    function cutText($string$setlength) {
        
    $length $setlength;
        if(
    $length<strlen($string)){
            while ((
    $string{$length} != " ") AND ($length 0)) {
                
    $length--;
            }
            if (
    $length == 0) return substr($string0$setlength);
            else return 
    substr($string0$length);
        }else return 
    $string;
    }
    ?>
    Last edited by xPlozion; 11-28-2008 at 02:55 PM.

  3. #3
    diabolo's Avatar
    diabolo is offline Community Advocate diabolo is on a distinguished road
    Join Date
    Nov 2007
    Location
    Jersey Shore
    Posts
    1,683

    Re: [PHP] cut off 160 chars

    that's what I was trying to fix, if it falls in the middle of the word it would cut off at the beginning of the word rather than the middle

  4. #4
    xPlozion's Avatar
    xPlozion is offline x10 Elder xPlozion is an unknown quantity at this point
    Join Date
    Mar 2008
    Location
    Delaware, USA
    Posts
    872

    Re: [PHP] cut off 160 chars

    found it at php.net. it's in my first post as an edit.
    Quote Originally Posted by xPlozion View Post
    PHP Code:
    <?php
    // Credit goes to: svihel
    // http://us3.php.net/manual/en/function.substr.php#84103

    function cutText($string$setlength) {
        
    $length $setlength;
        if(
    $length<strlen($string)){
            while ((
    $string{$length} != " ") AND ($length 0)) {
                
    $length--;
            }
            if (
    $length == 0) return substr($string0$setlength);
            else return 
    substr($string0$length);
        }else return 
    $string;
    }
    ?>
    hope that works for you.

  5. #5
    diabolo's Avatar
    diabolo is offline Community Advocate diabolo is on a distinguished road
    Join Date
    Nov 2007
    Location
    Jersey Shore
    Posts
    1,683

    Re: [PHP] cut off 160 chars

    thanks xPlozion
    hmm this brings me closer to my goal

  6. #6
    xPlozion's Avatar
    xPlozion is offline x10 Elder xPlozion is an unknown quantity at this point
    Join Date
    Mar 2008
    Location
    Delaware, USA
    Posts
    872

    Re: [PHP] cut off 160 chars

    anything to help ;)

  7. #7
    natsuki's Avatar
    natsuki is offline x10 Sophmore natsuki is an unknown quantity at this point
    Join Date
    Sep 2008
    Posts
    112

    Re: [PHP] cut off 160 chars

    There are many ways to do it and this one uses the function xPlozion posted. I could have made my own too.

    PHP Code:
    $lengthover strlen($message);

    $parts $lengthover 160;

    $beg 0;
    $msg = array();

    do 
    // or a for loop too
    {
        
    $msg_left substr($message$beg);

        
    $msg_part cutText($msg_left160);
        
    $msg[] = $msg_part;

        
    $beg += strlen($msg_part);

        
    $parts--;
    }
    while (
    $parts >= 0);
    // then access the $msg array using foreach or count() 
    I haven't tested it though..
    Last edited by natsuki; 11-28-2008 at 05:54 PM.

  8. #8
    xPlozion's Avatar
    xPlozion is offline x10 Elder xPlozion is an unknown quantity at this point
    Join Date
    Mar 2008
    Location
    Delaware, USA
    Posts
    872

    Re: [PHP] cut off 160 chars

    mind sharing function cutText()? although i think it just either returns one word separated by a space, or explodes it. i'm gonna go w/ the first

  9. #9
    mephis's Avatar
    mephis is offline x10Hosting Member mephis is an unknown quantity at this point
    Join Date
    Aug 2008
    Location
    London, UK
    Posts
    39

    Re: [PHP] cut off 160 chars

    regarding xPlozion's code, it would be faster to use a builtin php function instead of iterating:
    PHP Code:
    // instead of iterating like this
    while (($string{$length} != " ") AND ($length 0)) {
        
    $length--;
    }
    // call strrpos() function which returns
    // the last occurrence of " " in $string from $setlength (offset)
    $length strrpos($string" "$setlength); 
    -----Edit:
    Actually, I just came up with a recursive function to do just that :P
    PHP Code:
    function cut_text($string$setlength, &$outarray=array()) {
        if (
    strlen($string)<=$setlength) {
            
    $outarray[] = $string;
        } else {
            
    $tmpstr substr($string,0,$setlength);
            
    $length strrpos($tmpstr" ");
            
    $outarray[] = substr($tmpstr,0,$length);
            
    cut_text(trim(substr($string,$length)),$setlength,$outarray);
        }
    }

    // call function
    cut_text($message160$result);
    // result is stored as an array in $result
    var_dump($result); 
    Last edited by mephis; 11-29-2008 at 07:21 AM.

  10. #10
    natsuki's Avatar
    natsuki is offline x10 Sophmore natsuki is an unknown quantity at this point
    Join Date
    Sep 2008
    Posts
    112

    Re: [PHP] cut off 160 chars

    That's a nice idea using strrpos() instead of looping so I thought of using it as well. But it seems it's limited to single character needles or searches and will still need looping in what I'm trying to achieve. If you can think of a better way to do it, that would be new knowledge to me as well.

    I made a similar function but instead of breaking it at spaces, it tries to break the words at certain boundaries like punctuations. If the word is longer than the length specified then it will be split apart depending on the length.

    PHP Code:
    <?php

    // This function will try to split the string at word boundaries as much as possible.
    // If the string is longer than $len, it will be split into a max of $len characters per part.
    // returns an array of strings
    function split_str($str$len)
    {
        
    $temp = array();
        
    $max strlen($str);
        
        
    $len $len abs($len) : $max;
        
        if (
    $len >= $max)
        {
            
    $temp[] = $str;
            return 
    $temp;
        }
        
        
    $start 0;
        
    $end $len;
        
        do 
    // what makes things tricky is because strings like arrays start at 0
        
    {
            while ((
    $end $start) && !is_punct($str{$end}))
            {
                
    $end--;
            }
            
            if (
    $end $start)
            {
                
    $end = (($end $start) < $len) ? $end $end;
                
    $temp[] = substr($str$start, ($end $start));
            }
            else
            {
                
    $end += $len;
                
    $temp[] = substr($str$start$len);
            }      

            
    $start $end;
            
    $end += $len;
        }
        while (
    $start $max);
        
        return 
    $temp;
    }

    // some helper function, returns true if $c is a punctuation, otherwise, false
    function is_punct($c)
    {
        
    // change this regex to your liking:
        
    return preg_match('/^[`~@#%^&*()\-=_+[\]\\{}|;\':",.\/\<\>?\s]$/'$c);
    }

    // call it like: (just for testing)
    $s 'The quick brown fox jumps over the lazy dog. The lazy dog, jumps over the quick brown fox.';
    $t split_str($s10);

    // then access each like
    $i 0;
        
    foreach (
    $t as $v)
    {
        echo 
    $i++ . ' |' $v '|<br />'// I used | just to see the spaces
    }
    //var_dump($t);
    You can modify the regex to your liking if you want. This function prevents having to copy or create new strings everytime just to get the remaining string and also prevents any recursion. It simply uses the original string the whole time.

    This has been tested by me and removed all the possible bugs I can ever find. If you enter a negative length, ex: -10 will be treated as 10. Length of 0 will be treated as the length of the string.

    You call it like:
    PHP Code:
    $parts split_str($message160);
    // then do what you like with the $parts 
    Sorry for the short variable names, it seems I've been doing too much C++. ^^;

    EDIT: @xPlozion: the cutText() was the function you posted ;)
    Last edited by natsuki; 11-29-2008 at 04:05 PM.

+ Reply to Thread
Page 1 of 2 12 LastLast

Similar Threads

  1. [PHP] Variables in PHP
    By Bryon in forum Tutorials
    Replies: 15
    Last Post: 01-29-2009, 09:46 AM
  2. [PHP] PHP For Starters
    By Complex in forum Tutorials
    Replies: 24
    Last Post: 06-14-2008, 11:40 PM
  3. [PHP] Server Uptime
    By Stanz in forum Tutorials
    Replies: 17
    Last Post: 05-13-2006, 10:07 AM
  4. [PHP] Creating a File Upload Script
    By o0slowpaul0o in forum Tutorials
    Replies: 17
    Last Post: 10-05-2005, 01:39 AM
  5. [PHP] MySQL Login System
    By BrettFreeman in forum Tutorials
    Replies: 15
    Last Post: 05-06-2005, 11:10 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers