Jump to content

Help with small project


Doctor

Recommended Posts

Okay, let's say I have  a block of text like so;

 

 

00000000 00000000

00000000 00000000

00000000 00000000

00000000 00000000

00000000 00000000

00000000 00000000

 

 

I need to be able to add more/less digits to the text.

 

Like so;

 

0x00000000 0x00000000

0x00000000 0x00000000

0x00000000 0x00000000

0x00000000 0x00000000

0x00000000 0x00000000

 

etc

 

Also if it's greater than a certain number, to subtract. :3

 

How would I go about doing that?

 

 

Thank you,

 

Doctor

Link to comment
https://forums.phpfreaks.com/topic/222768-help-with-small-project/
Share on other sites

Your explanation isn't very precise. How about some more information?

 

Alright, I need to add 0x, infront of the sets of zero's. I know it doesn't make much sense.

 

For example, when I export the text, it's raw form is

 

01234567 01234567

00000000 00000000

 

Random numbers, for the sake of explanation.

 

I need to be able to add 0x in front of each set.

 

Like;

 

0x01234567 0x01234567

0x00000000 0x00000000

 

I hope that's enough. :3

This seems pretty simple looking at the code that you posted. You will simply need to do something like this:

 

$text = '000000 000000 000000 000000';
$new_text = '0x'.implode(' 0x',explode(' ',$text));
echo $new_text;

 

That will give you what you want. If this is coming from a file you will just have to read each of the lines and do this on it.

<?php

$data=<<<EOF
00000000 00000000
00000000 00000000
00000000 00000000
00000000 00000000
12345678 1234567890
1234 12
EOF;

function numfmt($val)
{
     return "0x". substr(str_repeat('0',.$val[0],-;
}

echo preg_replace_callback('/\b[\da-fA-F]{1,8}\b/','numfmt',$data);

 

in the 4 test case.

1) was had 8 hex digits (max digit length) so 0x was prefixed

2) had 10 hex digits (exceeds digit length) so was skipped)

3 & 4) didnt have enough hex digits so both were padded with 0's

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.