Doctor Posted December 28, 2010 Share Posted December 28, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/222768-help-with-small-project/ Share on other sites More sharing options...
requinix Posted December 28, 2010 Share Posted December 28, 2010 Your explanation isn't very precise. How about some more information? Quote Link to comment https://forums.phpfreaks.com/topic/222768-help-with-small-project/#findComment-1151954 Share on other sites More sharing options...
Doctor Posted December 28, 2010 Author Share Posted December 28, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/222768-help-with-small-project/#findComment-1151955 Share on other sites More sharing options...
Zurev Posted December 28, 2010 Share Posted December 28, 2010 This is a bit confusing without providing any code at all. If each of the random numbers is in an array you can loop through them and concatenate a 0x to them, however why they aren't generated with a 0x in the first place is what I'm wondering. Quote Link to comment https://forums.phpfreaks.com/topic/222768-help-with-small-project/#findComment-1151956 Share on other sites More sharing options...
johnny86 Posted December 28, 2010 Share Posted December 28, 2010 If you only need to add 0x in front of 0 you can just replace all 0 with 0x0: str_replace("0", "0x0", "0123456789 0123456789"); // 0x0123456789 0x0123456789 Quote Link to comment https://forums.phpfreaks.com/topic/222768-help-with-small-project/#findComment-1151961 Share on other sites More sharing options...
ngreenwood6 Posted December 28, 2010 Share Posted December 28, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/222768-help-with-small-project/#findComment-1151963 Share on other sites More sharing options...
laffin Posted December 28, 2010 Share Posted December 28, 2010 <?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 Quote Link to comment https://forums.phpfreaks.com/topic/222768-help-with-small-project/#findComment-1151972 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.