Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-17-2011, 10:48 PM   PM User | #1
victimist
New to the CF scene

 
Join Date: Dec 2011
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
victimist is an unknown quantity at this point
Post Splitting and rebuilding a string

Hi,

I have spent the last 3 hours trying to split a string in php and rebuild it to shorten it. The outcome always failed ...

I am farmialir with VB, and have googled up how to code php ... But still i am unable to code a solution. i have tried using list, split_str, split, explode to no avail. (Prob due to the fact that my php skills are 0)

I have a pre-existing php which generates an arp table. Im trying to split a mac address variable then build a new string which only contains the 1st 3 values of the split mac address. The VB equivalent would look something like

====

strStartString = "xx:xx:xx:xx:xx:xx"
arrStartString = split(strStartString,":")

If len(arrStartString(0)) = 1 then
arrStartString(0) = "0" & arrStartString(0)
End If
If len(arrStartString(1)) = 1 then
arrStartString(1) = "0" & arrStartString(1)
End If
If len(arrStartString(2)) = 1 then
arrStartString(2) = "0" & arrStartString(2)
End If

newStrString = arrStartString(0) & arrStartString(1) & arrStartString(2)

====

Desired result being:
Mac1 = 0:55:00:F0:AB:CD
Result1 = 005500

Mac2 = 0:C:5:22:32:F4
Result2 = 000C05

Sorry for the NOOB question

Thank you
victimist is offline   Reply With Quote
Old 12-17-2011, 11:43 PM   PM User | #2
mlseim
Master Coder

 
mlseim's Avatar
 
Join Date: Jun 2003
Location: Cottage Grove, Minnesota
Posts: 9,042
Thanks: 8
Thanked 1,029 Times in 1,020 Posts
mlseim has a spectacular aura aboutmlseim has a spectacular aura aboutmlseim has a spectacular aura about
PHP Code:
<?php

$strStartString 
"xx:xx:xx:xx:xx:xx";
$pieces=explode(":",$strStartString);

// now it's an array.
// here is how to loop through the array and do things with each piece.

$build1="";
$build2="";
$count=0;
foreach(
$pieces as $piece){
   if(
strlen($piece==1)){
   
$piece="0".$piece;
   }
      if(
$count<= 2){
      
$build1 .= $piece;
      }
      else{
      
$build2 .= $piece;
      }
}
echo 
$build1."   ".$build2;

?>
mlseim is offline   Reply With Quote
Old 12-18-2011, 10:13 AM   PM User | #3
victimist
New to the CF scene

 
Join Date: Dec 2011
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
victimist is an unknown quantity at this point
Thanks

Thank you very much mlseim.

Appreciate you pointing me in the right direction

Will give this a go today and post the outcome!


Seasons greetings to all!
victimist is offline   Reply With Quote
Old 12-18-2011, 10:52 AM   PM User | #4
victimist
New to the CF scene

 
Join Date: Dec 2011
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
victimist is an unknown quantity at this point
This is sort of working ... But its not creating a shortened string as i would have liked



the 2nd from the top has added an extra Zero - which is bang on. 0:1c:df:11:65:53 became 001cdf116553

But the very next one down didnt do it? 0:13:8f:f3:3f:3c became 0138ff33f3c and not 00138ff33f3c

Also, the new string still contains all charaters, i would have liked 0:1c:df:11:65:53 to become 001CDF and 0:13:8f:f3:3f:3c to become 00138F

Is this possible?

Thanks Guys!
victimist is offline   Reply With Quote
Old 12-18-2011, 11:11 AM   PM User | #5
victimist
New to the CF scene

 
Join Date: Dec 2011
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
victimist is an unknown quantity at this point
I have added a line to track the count and now i get a shorted string as desired. BUT, still no luck on adding a "0" to a single Character string?




Code:
$strStartString = $arpw_mac;
$pieces=explode(":",$strStartString);

// now it's an array.
// here is how to loop through the array and do things with each piece.

$build1="";
$build2="";
$count=0;
foreach($pieces as $piece){
   $count= $count+1;
   if(strlen($piece==1)){
   $piece="0".$piece;
   }
      if($count<= 3){
      $build1 .= $piece;
      }
      else{
      $build2 .= $piece;
      }
}

Last edited by victimist; 12-18-2011 at 11:17 AM..
victimist is offline   Reply With Quote
Old 12-18-2011, 11:42 AM   PM User | #6
BluePanther
Senior Coder

 
Join Date: Jul 2011
Posts: 1,226
Thanks: 3
Thanked 171 Times in 171 Posts
BluePanther is on a distinguished road
Quote:
Originally Posted by victimist View Post
I have added a line to track the count and now i get a shorted string as desired. BUT, still no luck on adding a "0" to a single Character string?




Code:
$strStartString = $arpw_mac;
$pieces=explode(":",$strStartString);

// now it's an array.
// here is how to loop through the array and do things with each piece.

$build1="";
$build2="";
$count=0;
foreach($pieces as $piece){
   $count= $count+1;
   if(strlen($piece==1)){
   $piece="0".$piece;
   }
      if($count<= 3){
      $build1 .= $piece;
      }
      else{
      $build2 .= $piece;
      }
}
Almost, just a misplaced bracket . But, you're using double the memory, and iterating more than needed.

My suggested solution:
PHP Code:
$strings explode(':',$arpw_mac);
// Leave only the first 3 indexes
array_splice($strings,3);
// Initialise result string
$result '';
// For all three indexes
foreach($strings as $string){
    if(
strlen($string) == 1){
        
$result .= '0';
    }
    
$result .= $string;

Not tested my solution, but should work as expected.
Edit: Decided to test it just in case, and it does work as expected
__________________
Useful function to retrieve difference in times
The best PHP resource
A good PHP FAQ
PLEASE remember to wrap your code in [PHP] tags.
PHP Code:
// Replace this
if(isset($_POST['submitButton']))
// With this
if(!empty($_POST))
// Then check for values/forms. Some IE versions don't send the submit button 
Quote:
Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.

Last edited by BluePanther; 12-18-2011 at 11:47 AM..
BluePanther is offline   Reply With Quote
Old 12-18-2011, 01:30 PM   PM User | #7
victimist
New to the CF scene

 
Join Date: Dec 2011
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
victimist is an unknown quantity at this point
Thanks BluePanther - Thats working perfectly

Appreciate your help
victimist is offline   Reply With Quote
Old 12-18-2011, 02:49 PM   PM User | #8
BluePanther
Senior Coder

 
Join Date: Jul 2011
Posts: 1,226
Thanks: 3
Thanked 171 Times in 171 Posts
BluePanther is on a distinguished road
Quote:
Originally Posted by victimist View Post
Thanks BluePanther - Thats working perfectly

Appreciate your help
No problem!
__________________
Useful function to retrieve difference in times
The best PHP resource
A good PHP FAQ
PLEASE remember to wrap your code in [PHP] tags.
PHP Code:
// Replace this
if(isset($_POST['submitButton']))
// With this
if(!empty($_POST))
// Then check for values/forms. Some IE versions don't send the submit button 
Quote:
Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.
BluePanther is offline   Reply With Quote
Old 12-18-2011, 05:02 PM   PM User | #9
kbluhm
Senior Coder

 
kbluhm's Avatar
 
Join Date: Apr 2007
Location: Philadelphia, PA, USA
Posts: 1,502
Thanks: 2
Thanked 258 Times in 254 Posts
kbluhm will become famous soon enough
Just curious if you had to mod that solution at all. I can't see how it works as desired if the return value of array_splice() isn't being captured. That is also a smaller point as I would expect array_slice() to be used instead, along with proper parameters. Lastly, the 0 for each single-character parts is being appended to the end rather than prepended to the front as your example states.

PHP Code:
$mac_addr '0:C:5:22:32:F4';

$parts explode':'$mac_addr );
$parts array_slice$parts0);

foreach ( 
$parts as & $part )
{
    
$part str_pad$part2'0'STR_PAD_LEFT );
}

$mac_addr_sub implode''$parts );

echo 
$mac_addr_sub// 000C05 
__________________
ZCE

Last edited by kbluhm; 12-18-2011 at 05:14 PM..
kbluhm is offline   Reply With Quote
Old 12-18-2011, 06:13 PM   PM User | #10
BluePanther
Senior Coder

 
Join Date: Jul 2011
Posts: 1,226
Thanks: 3
Thanked 171 Times in 171 Posts
BluePanther is on a distinguished road
Quote:
Originally Posted by kbluhm View Post
Just curious if you had to mod that solution at all. I can't see how it works as desired if the return value of array_splice() isn't being captured. That is also a smaller point as I would expect array_slice() to be used instead, along with proper parameters. Lastly, the 0 for each single-character parts is being appended to the end rather than prepended to the front as your example states.

PHP Code:
$mac_addr '0:C:5:22:32:F4';

$parts explode':'$mac_addr );
$parts array_slice$parts0);

foreach ( 
$parts as & $part )
{
    
$part str_pad$part2'0'STR_PAD_LEFT );
}

$mac_addr_sub implode''$parts );

echo 
$mac_addr_sub// 000C05 
Are you talking about my solution?

array_splice() returns the removed parts from the array - the parts after offset, for length (default 0), are removed then returned. We don't need them, so they can just go unassigned. I used splice over slice as it made sense to me - I won't be using the rest of the array so why not just get rid. Verified the parameters for array_splice() - only 2 parameters needed as the rest are optional, and the default values are what I want.

The 0 is being appended to the string just before the current array part, if it's required. It checks length of the current index, appends 0 to the string if needed, then appends the array index.

It works as expected:
http://codepad.org/c8d2pAgn
__________________
Useful function to retrieve difference in times
The best PHP resource
A good PHP FAQ
PLEASE remember to wrap your code in [PHP] tags.
PHP Code:
// Replace this
if(isset($_POST['submitButton']))
// With this
if(!empty($_POST))
// Then check for values/forms. Some IE versions don't send the submit button 
Quote:
Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.
BluePanther is offline   Reply With Quote
Reply

Bookmarks

Tags
array, rebuild, split, string

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 01:34 PM.


Advertisement
Log in to turn off these ads.