Brandon_R Posted July 8, 2009 Share Posted July 8, 2009 Hello PHP masters of phpfreaks i need help yet again. I need a code that : 1. Takes a string and separates it into 2 characters then a space then another 2 characters like this. 1234567890 will become 12 34 56 78 90 2. Reverse the above output. 12 34 56 78 90 will become 90 78 56 34 12 So finally when a user enters a string in the form on the html page such as 1234ABCD and clicks "reverse", they will see CD AB 34 12 on the outputted page. Also could you give me a code to validate their input to make sure it only consists of 0-9 and A-F please. Its for hex. Thanks You very Much If You decide To Help Me. Link to comment https://forums.phpfreaks.com/topic/165127-need-to-know-how-to-separe-and-reverse-a-string-in-php/ Share on other sites More sharing options...
Andy-H Posted July 8, 2009 Share Posted July 8, 2009 <?php $str = "12345678910"; $newStr = ""; for($i = 1; $i < strLen($str); $i++) { $newStr .= $str[$i - 1]; if ($i % 2 === 0) $newStr .= " "; } $newStr = strRev($newStr); ?> You could create an array of the codes and check if the current character in the loop is in the array using In Array or you could use regular expressions. Link to comment https://forums.phpfreaks.com/topic/165127-need-to-know-how-to-separe-and-reverse-a-string-in-php/#findComment-870703 Share on other sites More sharing options...
Brandon_R Posted July 8, 2009 Author Share Posted July 8, 2009 Thanks i am very NEW to PHP. How do i output it so people can view it on an HTML page. TYVM sir. Link to comment https://forums.phpfreaks.com/topic/165127-need-to-know-how-to-separe-and-reverse-a-string-in-php/#findComment-870818 Share on other sites More sharing options...
AwptiK Posted July 8, 2009 Share Posted July 8, 2009 echo $newStr; Put that at the very bottom of the code, under the $newStr = strRev($newStr); EDIT: Actually, you have to grab the value from the form and use it as the value of $str at the top of the code. I didn't notice the code at the beginning was just an example and you needed more direction. Ex <html> <head><title>Hex</title></head> <body> <form action="hex.php" method="post"> Enter: <input type="text" name="hex" /> <input type="submit" value="Submit" /> </form> </body> </html> and the hex.php <?php $str = $_POST['hex']; $newStr = ""; for($i = 1; $i < strLen($str); $i++) { $newStr .= $str[$i - 1]; if ($i % 2 === 0) $newStr .= " "; } $newStr = strRev($newStr); echo $newStr; ?> Link to comment https://forums.phpfreaks.com/topic/165127-need-to-know-how-to-separe-and-reverse-a-string-in-php/#findComment-870821 Share on other sites More sharing options...
Philip Posted July 8, 2009 Share Posted July 8, 2009 Or using native PHP functions: <?php // Our string $string = '1234567890'; // show the string, plus line break echo $string.'<br>'; // Create an array element for every 2 characters, then implode it with a space. $withSpaces = implode(' ', str_split($string, 2)); // show the string with spaces, plus line break echo $withSpaces.'<br>'; // Create our reversed string $revString = strrev($string); // show the reversed string, plus line break echo $revString.'<br>'; // Create an array element for every 2 characters of our reversed string, then implode $withSpacesRev = implode(' ', str_split($revString,2)); // show the reversed string with spaces echo $withSpacesRev; ?> Outputs: 1234567890 12 34 56 78 90 0987654321 09 87 65 43 21 Which, technically doesn't do exactly what you want. "12 34 56 78 90 will become 90 78 56 34 12" - did you mean reverse the order of the 'sets', or reverse all the numbers? Link to comment https://forums.phpfreaks.com/topic/165127-need-to-know-how-to-separe-and-reverse-a-string-in-php/#findComment-870822 Share on other sites More sharing options...
Brandon_R Posted July 8, 2009 Author Share Posted July 8, 2009 @KingPhilip After the string is convert into sets of 2, they are reversed in sets of 2, not individually. @Everyone who helped me, much appreciated. Link to comment https://forums.phpfreaks.com/topic/165127-need-to-know-how-to-separe-and-reverse-a-string-in-php/#findComment-870863 Share on other sites More sharing options...
thebadbad Posted July 8, 2009 Share Posted July 8, 2009 Or <?php $str = '1234567890'; //validate input if (!ctype_xdigit($str)) { echo 'Entered string must only consist of hexadecimal digits.'; } //create reversed set $set = implode(' ', array_reverse(str_split($str, 2))); echo $set; ?> Link to comment https://forums.phpfreaks.com/topic/165127-need-to-know-how-to-separe-and-reverse-a-string-in-php/#findComment-870867 Share on other sites More sharing options...
Brandon_R Posted July 8, 2009 Author Share Posted July 8, 2009 Thebadbad thank you for the validation Link to comment https://forums.phpfreaks.com/topic/165127-need-to-know-how-to-separe-and-reverse-a-string-in-php/#findComment-871518 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.