SitePoint Sponsor

User Tag List

Results 1 to 22 of 22

Thread: Varible split

Hybrid View

  1. #1
    SitePoint Zealot
    Join Date
    Mar 2006
    Posts
    133
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Varible split

    Hi, I am selecting a very long string out of a database such as story
    I want to display the story in three table columns next to each other.
    How can i count how many characters are in a varible and split into three equal(or near equal) varibles. i.e.:

    $var1=nice sunny day
    turns into
    $var2=nice
    $var3=sunny
    $var4=day

    It doesnt matter if it cuts in the middle of a word.

    any ideas?

    thanks in advance

  2. #2
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,578
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    As long as you don't care about word boundaries...

    count($var1) would give you the number of characters
    substr($var1, start, length) gives you a substring

  3. #3
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    strlen($str) will give you the number of characters in the string--count() will not.

  4. #4
    SitePoint Zealot
    Join Date
    Mar 2006
    Posts
    133
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    And how would i split it up into three?

  5. #5
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,578
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Divide by 3 and use that as your length in the substr()'s.

  6. #6
    SitePoint Zealot
    Join Date
    Mar 2006
    Posts
    133
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok thanks

  7. #7
    SitePoint Zealot
    Join Date
    Mar 2006
    Posts
    133
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok i understand that

    strlen($var1);

    will give me the number of characters. but what does it asign that number to? i.e. a varible or what?

    so i can do

    $legnth / 3

    can i do it like this:

    $length = strlen($var1);

    Thanks

    David

  8. #8
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    will give me the number of characters. but what does it asign that number to? i.e. a varible or what?
    If the function returns the count, then you want to store that somewhere so you can use it. In computer programming languages, you store values in what are called 'variables', e.g.:

    $totalLength = strlen($myString);

    The strlen() function figures out the count for you, and that line tells php to store the value in the variable $totalLength. That is what the '=' does. It says to 'assign' the value on the right, to the variable on the left.

  9. #9
    SitePoint Zealot
    Join Date
    Mar 2006
    Posts
    133
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok thanks

  10. #10
    SitePoint Zealot
    Join Date
    Mar 2006
    Posts
    133
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I got it working.
    just incase anybody else wants to use it:

    PHP Code:
    $result = @mysql_query("SELECT subject, maintext FROM article where id='$artid'"); 
     if (!
    $result) { 
       exit(
    '<p>Error performing query: ' 
           
    mysql_error() . '</p>'); 
     } 
    while (
    $row mysql_fetch_array($result)) {  
       
    $subject $row['subject']; 
       
    $maintext $row['maintext']; 
     } 
     
     
    $totallength strlen($maintext);
     
    $shortlength $totallength 3;
     
    $longlength $shortlength 2;
     
    $part1 substr($maintext,0,$shortlength);
     
    $part2 substr($maintext,$shortlength,$shortlength);
     
    $part3 substr($maintext,$longlength,$shortlength); 
    Thanks so much.

  11. #11
    SitePoint Member
    Join Date
    Apr 2006
    Posts
    21
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    You might also try something like this. It'll split on spaces, and it'll look nicer (even though I know you said this wasn't important)

    PHP Code:
    $text explode (' '$text);
    $text array_chunk ($text, (count ($text) / 3) + 1);
    for (
    $i=0$i<3; ++$i$text[$i] = implode (' '$text[$i]);

    // Access the text for the 3 columns like:
    echo $text[0];
    echo 
    $text[1];
    echo 
    $text[2]; 
    Last edited by exam; Apr 24, 2006 at 10:26. Reason: oops

  12. #12
    SitePoint Zealot
    Join Date
    Mar 2006
    Posts
    133
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by exam
    You might also try something like this. It'll split on spaces, and it'll look nicer (even though I know you said this wasn't important)

    PHP Code:
    $text explode (' '$text);
    $text array_chunk ($text, (count ($text) / 3) + 1);

    // Access the text for the 3 columns like:
    echo $text[0];
    echo 
    $text[1];
    echo 
    $text[2]; 
    Thanks i will use this instead.
    one question though.

    it doesnt work.

    when i do echo $text[0]; or any of them, it just sais "Array" on the screen.

    any ideas?

  13. #13
    SitePoint Member
    Join Date
    Apr 2006
    Posts
    21
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Glad you got it working, although I believe what I posted is a more readable solution.

  14. #14
    SitePoint Member
    Join Date
    Apr 2006
    Posts
    21
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    I just fixed it. That's what I get for typing and not testing first.

  15. #15
    SitePoint Zealot
    Join Date
    Mar 2006
    Posts
    133
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That works perfect and its the code is neater than mine.

    Thank you so much.

  16. #16
    SitePoint Member
    Join Date
    Apr 2006
    Posts
    21
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Your code is fine too, many coders fight over what's "better" when it's really a subjective thing. What would tip the scales for me is that it splits between words.

  17. #17
    SitePoint Zealot
    Join Date
    Mar 2006
    Posts
    133
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes thats what did it for me too.
    Thats the first compliment ive had about my coding!
    ussualy its all over the place.

  18. #18
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Your code is fine too,
    Actually, I don't think it is. I think it will drop some characters in the middle and on the end. Oh, well.

  19. #19
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try this:
    PHP Code:
    $maintext "hello world";

    $totallength strlen($maintext);
    $shortlength $totallength 3;
    $longlength $shortlength 2;
    $part1 substr($maintext,0,$shortlength);
    $part2 substr($maintext,$shortlength,$shortlength);
    $part3 substr($maintext,$longlength,$shortlength); 

    echo 
    $part1;
    echo 
    $part2;
    echo 
    $part3
    It certainly could be fixed by rounding $shortlength, and using:

    substr($maintext,$longlength)

    to get everything to the end of the string for the last piece. The substring method will be more efficient, although the explode(), array_chunk(), and implode() is a clever script.

    You got a lot a farther than I thought you would ever get after your initial questions.

  20. #20
    SitePoint Zealot
    Join Date
    Mar 2006
    Posts
    133
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Told you, i knew my coding couldnt be that good!

  21. #21
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Me01273
    Hi, I am selecting a very long string out of a database such as story
    I want to display the story in three table columns next to each other.
    How can i count how many characters are in a varible and split into three equal(or near equal) varibles. i.e.:

    $var1=nice sunny day
    turns into
    $var2=nice
    $var3=sunny
    $var4=day

    It doesnt matter if it cuts in the middle of a word.

    any ideas?

    thanks in advance
    PHP Code:
    $text "hello world";

    var_dump(
        
    explode(
            
    "\000"
            
    trim(
                
    chunk_split($textstrlen($text) / 3"\000")
            )
        )
    ); 
    chunk_split

  22. #22
    SitePoint Member
    Join Date
    Apr 2006
    Posts
    21
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by stereofrog
    PHP Code:
    $text "hello world";

    var_dump(
        
    explode(
            
    "\000"
            
    trim(
                
    chunk_split($textstrlen($text) / 3"\000")
            )
        )
    ); 
    chunk_split
    Looks pretty clever, I like it.


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
  •