uppercasefive Posted June 2, 2009 Share Posted June 2, 2009 So I've got a string $string = "1234567"; I want to randomly pad it with random characters from an array, example: $string = "12xa3b45ds67"; I can't think of a good way to accomplish this, at least not without excessive code. Is there a PHP function to do this, or will I have to find one? Link to comment https://forums.phpfreaks.com/topic/160690-randomly-pad-a-string/ Share on other sites More sharing options...
uppercasefive Posted June 2, 2009 Author Share Posted June 2, 2009 Could I use preg_replace(..) to just add things every x characters or so? edit: ahh crap meant to hit edit /sigh Link to comment https://forums.phpfreaks.com/topic/160690-randomly-pad-a-string/#findComment-848034 Share on other sites More sharing options...
Alex Posted June 2, 2009 Share Posted June 2, 2009 Edit:: Wait that's horrible let me edit it. Link to comment https://forums.phpfreaks.com/topic/160690-randomly-pad-a-string/#findComment-848042 Share on other sites More sharing options...
Alex Posted June 2, 2009 Share Posted June 2, 2009 Try: function pad_string($str, $arr) { $amt=(strlen($str)/2); $split = str_split($str); $ori = $split; for($i = 0;$i < $amt;$i++) { $replace = rand(0, count($split)); array_splice($split, $replace, 0, $arr[rand(0, count($arr))]); } return implode('', $split); } $arr = array('x', 'b', 'c', 'd'); $str = '436275547'; $new_str = pad_string($str, $arr); echo 'Original String: ' . $str . '<br />'; echo 'Original Length: ' . strlen($str) . '<br />'; echo 'New String: ' . $new_str . '<br />'; echo 'New String Length: ' . strlen($new_str) . '<br />'; Output: Original String: 4362755647 Original Length: 10 New String: 4c36275564b7cdx New String Length: 15 Link to comment https://forums.phpfreaks.com/topic/160690-randomly-pad-a-string/#findComment-848057 Share on other sites More sharing options...
Alex Posted June 2, 2009 Share Posted June 2, 2009 Arg, I edited that last post wrong.. And now I can't edit it again.. this is it: function pad_string($str, $arr) { $amt = (strlen($str)/2); $split = str_split($str); $num = 0; while($num < $amt) { array_splice($split, rand(0, count($split)), 0, $arr[rand(0, count($arr))]); $num++; } return implode('', $split); } $arr = array('x', 'b', 'c', 'd'); $str = '4362755647'; $new_str = pad_string($str, $arr); echo 'Original String: ' . $str . '<br />'; echo 'Original Length: ' . strlen($str) . '<br />'; echo 'New String: ' . $new_str . '<br />'; echo 'New String Length: ' . strlen($new_str) . '<br />'; Output: Original String: 4362755647 Original Length: 10 New String: 4c36275564b7cdx New String Length: 15 Link to comment https://forums.phpfreaks.com/topic/160690-randomly-pad-a-string/#findComment-848064 Share on other sites More sharing options...
uppercasefive Posted June 2, 2009 Author Share Posted June 2, 2009 I figured it out... somewhat. TY Alex for the help for($i = 0; $i < 6; $i++) { $x = rand(0, 2); if($x == 1) $code .= $this->letters[rand(0, 13)] . $str[$i]; else if($x == 2) $code .= $str[$i] . $this->letters[rand(0, 13)]; else $code .= $str[$i]; } Link to comment https://forums.phpfreaks.com/topic/160690-randomly-pad-a-string/#findComment-848067 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.