Jump to content

Need To know How To Separe And Reverse A String In PHP


Brandon_R

Recommended Posts

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
Share on other sites

<?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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.