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
https://forums.phpfreaks.com/topic/22105-string-formatting-help-please/
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]
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 
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]

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.