Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    New to the CF scene
    Join Date
    Dec 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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

  • #2
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,493
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    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;

    ?>

  • #3
    New to the CF scene
    Join Date
    Dec 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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!

  • #4
    New to the CF scene
    Join Date
    Dec 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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!

  • #5
    New to the CF scene
    Join Date
    Dec 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #6
    Senior Coder
    Join Date
    Jul 2011
    Posts
    1,226
    Thanks
    3
    Thanked 171 Times in 171 Posts
    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
    Last edited by BluePanther; 12-18-2011 at 11:47 AM.
    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 
    Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.

  • #7
    New to the CF scene
    Join Date
    Dec 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks BluePanther - Thats working perfectly

    Appreciate your help

  • #8
    Senior Coder
    Join Date
    Jul 2011
    Posts
    1,226
    Thanks
    3
    Thanked 171 Times in 171 Posts
    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 
    Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.

  • #9
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    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 
    Last edited by kbluhm; 12-18-2011 at 05:14 PM.

  • #10
    Senior Coder
    Join Date
    Jul 2011
    Posts
    1,226
    Thanks
    3
    Thanked 171 Times in 171 Posts
    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 
    Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •