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
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
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..
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
// 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..
// 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.
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.
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.
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.
// 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.