Skip to content

Instantly share code, notes, and snippets.

@michvaldes001
Last active March 9, 2016 16:22
Show Gist options
  • Save michvaldes001/f8ef4e7671204b43a2b7 to your computer and use it in GitHub Desktop.
Save michvaldes001/f8ef4e7671204b43a2b7 to your computer and use it in GitHub Desktop.
PHP Text to DNA encoder.
<?php
#Demo here: http://michvaldes001.ddns.net/index.php?section_id=Web_Apps&app_id=text_to_dna&name=Testing
#Generate user input box.
echo "<h3>Please Enter Your First Name</h3>
<form id='name_form' action = '<LOCATION OF TEXT_TO_DNA_ENCODE_REDIRECT.PHP>'>
<input name='name_submit' type='text' id='name_s'>
<button type='submit'>Submit</button>
</form>";
#Convert name into hexadecimal and then binary.
$input_string = $_GET["name"];
$binary_string_read = unpack('H*', $input_string);
$binary_string = base_convert($binary_string_read[1], 16, 2);
#Display name.
echo "<h3>Your Name is:</h3>";
echo $input_string;
$binary_string_legnth = strlen($binary_string);
#Ensure that binary string legnth is an odd numver, pad the leading bit with a zero if not.
if ($binary_string_legnth % 2 != 0)
{
$binary_string = str_pad($binary_string, ($binary_string_legnth + 1), "0", STR_PAD_LEFT);
}
#Display name in binary.
echo "<h3>Your Name in Binary is:</h3>";
echo $binary_string;
echo "<br>";
#Inform user how the base pair encoding method works.
echo "<h3>Encode Method:</h3>";
echo "00 = A-T, 01 = T-A, 10 = C-G, 11 = G-C";
#Begin base pair display.
echo "<h3>Your Name in DNA Base Pairs is:</h3>";
echo "<div style='width: 250px; height: auto;'>";
#Split string into chunks of two characters (00, 01, 10, or 11).
for ($i = 0; $i < strlen($binary_string); $i = $i + 2)
{
$sort_binary_string = substr($binary_string, $i,($i + 2));
$base_pair_numbers = substr($sort_binary_string, 0, 2);
switch($base_pair_numbers){
#Display base pair that corresponds to each binary chunk.
#Note, you will have to save images from the demo.
case "00": echo "<img src = '<DIRECTORY OF THE IMAGE>/AT.jpg'>";
case "01": echo "<img src = '<DIRECTORY OF THE IMAGE>/TA.jpg'>";
case "10": echo "<img src = '<DIRECTORY OF THE IMAGE>/CG.jpg'>";
case "11": echo "<img src = '<DIRECTORY OF THE IMAGE>/GC.jpg'>";
}
}
echo "</div>";
?>
<?php
#Read name sent from PHP main file and redirect back.
$input_name_redirect_string = $_GET["name_submit"];
echo "<html><body><meta http-equiv='refresh' content='0;URL=<LOCATION OF TEXT_TO_DNA_ENCODE.PHP>?name=" . $input_name_redirect_string . "'></body></html>" ;
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment