SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    SitePoint Member
    Join Date
    Sep 2004
    Location
    London
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Splitting a text String

    Hi All,

    I'm trying to split a text string of 250 chars length into 50 char chunks. I know this part is easy, however i want to split the string at the nearest available space either side of 50 chars so as not to truncate a word.

    Can anyone help please.

    Thanks

  2. #2
    Tranceoholic lilleman's Avatar
    Join Date
    Feb 2004
    Location
    Írebro, Sweden
    Posts
    2,716
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    The code below has not been tested, and there might be a smoother/better way to do it.

    PHP Code:
    <?php

    // I assume that your string is stored in $string.

    $chunks = array('');
    $current 0;

    $words explode(' '$string);

    for( 
    $b 0$b sizeof($words); $b++ )
    {
      
    $chunks[$current] .= $words[$b].' ';
      
      if( 
    strlen($chunks[$current]) >= 50 )
        
    $chunks[++$current] = '';
    }

    ?>
    Yours, Erik.
    ERIK RIKLUND :: Yes, I've been gone quite a while.

  3. #3
    SitePoint Member
    Join Date
    Sep 2004
    Location
    London
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi eirk,

    thanks for getting back to me so quick, im having a problem with the example.

    I get an unexpected T_variable for the following line:

    **$chunks[$current] .= $words[$b].' ';

    Sorry to be a pain, can you help?

    Cheers

  4. #4
    Tranceoholic lilleman's Avatar
    Join Date
    Feb 2004
    Location
    Írebro, Sweden
    Posts
    2,716
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    That is strange... I tried the code on my local machine just now, and it worked as it was supposed to.
    Could you post your full code here?

    Yours, Erik.
    ERIK RIKLUND :: Yes, I've been gone quite a while.

  5. #5
    SitePoint Member
    Join Date
    Sep 2004
    Location
    London
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    <?php

    $string = 'this is a test to see if the script works and splits the text accordingly after 50 chars';

    $chunks = array('');
    $current = 0;

    $words = explode(' ', $string);

    for( $b = 0; $b < sizeof($words); $b++ )
    {
    $chunks[$current] .= $words[$b].' ';

    if( strlen($chunks[$current]) >= 50 )
    $chunks[++$current] = '';
    }

    ?>

  6. #6
    Tranceoholic lilleman's Avatar
    Join Date
    Feb 2004
    Location
    Írebro, Sweden
    Posts
    2,716
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    The code you posted works for me.

    Yours, Erik.
    ERIK RIKLUND :: Yes, I've been gone quite a while.

  7. #7
    SitePoint Member
    Join Date
    Sep 2004
    Location
    London
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i dont understand then

  8. #8
    Tranceoholic lilleman's Avatar
    Join Date
    Feb 2004
    Location
    Írebro, Sweden
    Posts
    2,716
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    That makes two of us. Which PHP version are you using?

    Yours, Erik.
    ERIK RIKLUND :: Yes, I've been gone quite a while.

  9. #9
    Umm. PHP Guru....Naaaah jaswinder_rana's Avatar
    Join Date
    Jul 2004
    Location
    canada
    Posts
    3,193
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i din't get any errors. worked for me too. and i have PHP 4.3.11

  10. #10
    SitePoint Member
    Join Date
    Sep 2004
    Location
    London
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think it must be something in my PHP.INI ... not sure what though ... any ideas would help. Not that i dont appreciate the help ive already had.

    Cheers

  11. #11
    SitePoint Wizard swdev's Avatar
    Join Date
    Oct 2004
    Location
    UK
    Posts
    1,053
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Works for me too,

    PHP 4.3.8
    Apache 2.0.52
    Win XP Pro SP2
    Last edited by swdev; Jan 5, 2005 at 04:32. Reason: added more config info


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •