Jump to content

String formatting help! Please.


ecos

Recommended Posts

Hello all,
I am trying to format a string such as there are, 2 characters seperated by a space for 16 two character sets and then a new line.
Example:
54 68 65 20 75 6e 6b 6e 6f 77 6e 20 6d 65 73 73
61 67 65 20 69 73 3a 20 54 68 65 20 44 45 53 2d
74 65 73 74 20 63 6f 6e 74 65 73 74 27 73 20 70
6c 61 69 6e 74 65 78 74 08 08 08 08 08 08 08 08

I have tried grouping everyother character, add a space between them and use chunk_split to make the correct length with a \r\n at the end, but the extra spaces I added are not trimmed. 

Any help would be GREATLY appreciated.
THanks,
Ecos
Link to comment
Share on other sites

[code]<?php
$variable = "all that information you had";
$variable = explode(0,2, $variable); // I think this will cut it like that, something with explode, implode, or substr, don't feel like looking right now
$variable = wordwrap();
// you just have to enter the parameters for word wrap
?>
That should put you in the right direction at least[/code]
Link to comment
Share on other sites

businessman332211,
Thanks for the quick reply, but with my limited experience, this code is not quite doing it.  Explode returns an array [array explode ( string separator, string string [, int limit] )] and wordrap() accepts only strings.  implode and substr don't jump out as winners either. 

Sorry if I misunderstand your insructions.
Thanks,
Ecos 
Link to comment
Share on other sites

try

[code]<?php

$str = '54686520756e6b6e6f776e206d6573736167652069733a20546865204445532d7465737420636f6e74657374277320706c61696e746578740808080808080808';

$new = explode('|', wordwrap($str,32,'|',true));
foreach($new as $k => $line) {
    $new[$k] = wordwrap($line,2,' ',true);
}
$result = join("\n", $new);

// view result
echo '<pre>',$result,'</pre>';
?>[/code]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.