SitePoint Sponsor

User Tag List

Results 1 to 11 of 11

Thread: Wordwrap Help

  1. #1
    SitePoint Enthusiast XploreR's Avatar
    Join Date
    May 2001
    Location
    Singapore
    Posts
    91
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Wordwrap Help

    Hello all.

    i got this line of data (database field $Address):

    0D1BC06000022820011BC0600002282A

    I would like to seperate them in 8s so they become like this:

    0D1BC060
    00022820
    011BC060
    0002282A

    I used this code:

    $Newaddress = wordwrap( $Address, 8, "<br>", 1);

    But the outpur turns out like this:

    0D1BC060
    000228200
    11BC06000
    02282A
    ANy Help?

  2. #2
    <? echo "Kick me"; ?> petesmc's Avatar
    Join Date
    Nov 2000
    Location
    Hong Kong
    Posts
    1,508
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Put double quotes (") around $Address in the wordwrap function.

    -Peter

  3. #3
    SitePoint Enthusiast XploreR's Avatar
    Join Date
    May 2001
    Location
    Singapore
    Posts
    91
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    it dosen't work either. Any others who can help me?

    My Code:


    $query4 = "SELECT * FROM Dreamcast.id$id";

    $db->query($query4);

    while ($db->next_record()) {

    $Cid = $db->f(Cid);
    $Description = $db->f(Description);
    $Address = $db->f(Address);
    $Notes = $db->f(Notes);
    $Description = ucwords($Description);
    $Newaddress = wordwrap( $Address, 8, "<br>", 1);

    $color = ($i++ % 2 ) ? '#222222' : '#333333';

    $tmp3 = "<tr bgcolor='$color'><td>&nbsp;<font size='2' face='Verdana, Arial'>$Cid</font></td><td>&nbsp;<font size='2' face='Verdana, Arial'>$Description</font></td><td><font size='2' face='Verdana, Arial'>$Newaddress</font></td><td>&nbsp;<font size='2' face='Verdana, Arial'>$Notes</a></font></td></tr>";

    $t->set_var('data', $tmp3);
    $t->parse('CBlock', 'CodesBlock', true);
    }

  4. #4
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    $Address = "0D1BC06000022820011BC0600002282A";
    $NewAddress = implode("<br>", split("\r\n", chunk_split($Address, 8)));
    print $data;
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  5. #5
    SitePoint Enthusiast XploreR's Avatar
    Join Date
    May 2001
    Location
    Singapore
    Posts
    91
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks, freedydoesphp. ur code works. bril!

    Something more difficult:

    Example:

    $Address = "0D1BC06000022820011BC0600002282ABCDE";

    $NewAddress = implode("<br>", split("\r\n", chunk_split($Address, 12)));

    well, let say after spliting $Address into 12s, now i want to add a space after the 8th character so that is now look like this:

    After Spliting into 12s:

    0D1BC0600002
    2820011BC060
    0002282ABCDE

    After Adding a space after the 8th character:

    0D1BC0600002
    2820011BC060
    0002282ABCDE

    Is there any way to advance from here or would i need a new code?

  6. #6
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't think your post reflects your question. I can't see a space in either one of those lists. Is it just supposed to be 8 characters a space then the rest up to 12?
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  7. #7
    SitePoint Enthusiast XploreR's Avatar
    Join Date
    May 2001
    Location
    Singapore
    Posts
    91
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    perhaps i'm confusing you, sorry

    well, i'm now trying to seperate my code in 12s for another script of mine. After spliting them , i would like to add a space at each of the code that have been split , after the 8th character.

    and this should be the corrected code:


    After Adding a space after the 8th character:

    0D1BC060 0002
    2820011B C060
    0002282A BCDE
    Last edited by XploreR; Jun 26, 2001 at 11:15.

  8. #8
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Shouldn't be too much more work to do that, I did have to restructure the logic a tad.

    PHP Code:
        function set_space(&$item$key) {
            
    $item ereg_replace("^([[:alnum:]]{8})""\\1 "$item);
            }

    $Address "0D1BC06000022820011BC0600002282ABCDE"
    $NewAddress split("\r\n"chunk_split($Address12));
    array_walk($NewAddress'set_space');
    reset($NewAddress);
    $NewAddress implode("<br>"$NewAddress); 
    print 
    $NewAddress
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  9. #9
    SitePoint Enthusiast XploreR's Avatar
    Join Date
    May 2001
    Location
    Singapore
    Posts
    91
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey freddydoesphp,

    i copied ur code had a test to see the output and it returns:

     0002
     C060
     BCDE

    The first 8 characters (digits) turns to .

    Any help?

  10. #10
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes I know exactly what happened, when I posted my code above vb cut off one of the backslashes before the 1 in this line:
    $item = ereg_replace("^([[:alnum:]]{8})", "\1 ", $item);

    There should be two backslashes before the number 1 like:
    $item = ereg_replace("^([[:alnum:]]{8})", "\\1 ", $item);
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  11. #11
    SitePoint Columnist Skunk's Avatar
    Join Date
    Jan 2001
    Location
    Lawrence, Kansas
    Posts
    2,066
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry to but in to this thread, but I just wanted to say that I really like this bit of code:
    PHP Code:
    $color = ($i++ % ) ? '#222222' '#333333'
    Has to be the shortest, tidiest method I've seen for alternating the background colours of table rows (I assume that is what it's for) - very nice indeed


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
  •