Page 1 of 1

How to display results PHP ? (Noob Question)

Posted: Sun Jan 23, 2011 1:13 pm
by AutoRunway
Got some PHP code to generate typos. Now I just need to get and display on page. Sorry I'm such a dun dun :banghead: Please help

Code: Select all

<?php
/*
Typo Generator Class

- PHP 5 is required
- The class is not intended to be used for the construction of an object but rather as a namespace
- The class has four methods each of which accept a string and return and array of strings that are likely typos of the type that particular function producesi
- Copyright with the the MIT License
- Developer was Scott Horne of Takeshi Media and Web-Professor.net
- http://web-professor.net for mor info


Class Functions:
-----------------------------------------------------------

cTypoGenerator::getWrongKeyTypos( $word )
	Typos based on a user hitting the wrong key that is near the intended key, only uses characters valid in ascii domain names 

cTypoGenerator::getMissedCharTypos( $word )
	Typos based on a missed key

cTypoGenerator::getTransposedCharTypos( $word )
	Typos based on transposition errors 

cTypoGenerator::getDoubleCharTypos( $word )
	Typos based on hitting an intended key twice

cTypoGenerator::getAllTypos( $word )
	This calls all the typos and returns every variety


Example Usage:
-----------------------------------------------------------
$word = "Hello";
$typos = array();
$typos = cTypoGenerator::getAllTypos( $word );

print_r( $typos );







Copyright (c) 2006, Takeshi Media

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 

*/




class cTypoGenerator
{

// array of keys near character on a QWERTY keyboard
// only valid characters in a domain name
	static $keyboard = array(
// top row
		'1' => array( '2', 'q' ),
		'2' => array( '1', 'q', 'w', '3' ),
		'3' => array( '2', 'w', 'e', '4' ),
		'4' => array( '3', 'e', 'r', '5' ),
		'5' => array( '4', 'r', 't', '6' ),
		'6' => array( '5', 't', 'y', '7' ),
		'7' => array( '6', 'y', 'u', '8' ),
		'8' => array( '7', 'u', 'i', '9' ),
		'9' => array( '8', 'i', 'o', '0' ),
		'0' => array( '9', 'o', 'p', '-' ),
		'-' => array( '0', 'p' ),
// 2nd from top
		'q' => array( '1', '2', 'w', 'a' ),
		'w' => array( 'q', 'a', 's', 'e', '3', '2' ),
		'e' => array( 'w', 's', 'd', 'r', '4', '3' ),
		'r' => array( 'e', 'd', 'f', 't', '5', '4' ),
		't' => array( 'r', 'f', 'g', 'y', '6', '5' ),	
		'y' => array( 't', 'g', 'h', 'u', '7', '6' ),
		'u' => array( 'y', 'h', 'j', 'i', '8', '7' ),
		'i' => array( 'u', 'j', 'k', 'o', '9', '8' ),
		'o' => array( 'i', 'k', 'l', 'p', '0', '9' ),
		'p' => array( 'o', 'l', '-', '0' ),
// home row
		'a' => array( 'z', 's' , 'w', 'q' ),
		's' => array( 'a', 'z', 'x', 'd', 'e', 'w' ),
		'd' => array( 's', 'x', 'c', 'f', 'r', 'e' ),
		'f' => array( 'd', 'c', 'v', 'g', 't', 'r' ),
		'g' => array( 'f', 'v', 'b', 'h', 'y', 't' ),
		'h' => array( 'g', 'b', 'n', 'j', 'u', 'y' ),
		'j' => array( 'h', 'n', 'm', 'k', 'i', 'u' ),
		'k' => array( 'j', 'm', 'l', 'o', 'i' ),
		'l' => array( 'k', 'p', 'o' ),
// bottom row
		'z' => array( 'x', 's', 'a' ),
		'x' => array( 'z', 'c', 'd', 's' ),
		'c' => array( 'x', 'v', 'f', 'd' ),
		'v' => array( 'c', 'b', 'g', 'f' ),
		'b' => array( 'v', 'n', 'h', 'g' ),
		'n' => array( 'b', 'm', 'j', 'h' ),
		'm' => array( 'n', 'k', 'j' )
	);

	function getAllTypos( $word )
	{
		$typos = array();

		$typos = array_merge( $typos, cTypoGenerator::getWrongKeyTypos($word));
		$typos = array_merge( $typos, cTypoGenerator::getMissedCharTypos($word));
		$typos = array_merge( $typos, cTypoGenerator::getTransposedCharTypos( $word ));
		$typos = array_merge( $typos, cTypoGenerator::getDoubleCharTypos( $word ));

		return $typos;
	}

// accepts a string
// returns array of likely single "wrong key" typos
// arrays contain only characters that are valid domain names

	function getWrongKeyTypos( $word )
	{
		$word = strtolower( $word );
		$typos = array();
		$length = strlen( $word );
// check each character
		for( $i = 0; $i < $length; $i++ )
		{
// if character has replacements then create all replacements
			if( cTypoGenerator::$keyboard[$word{$i}] )
			{
// temp word for manipulating
				$tempWord = $word;
				foreach( cTypoGenerator::$keyboard[$word{$i}] as $char )
				{
					$tempWord{$i} = $char;			
					array_push( $typos, $tempWord );
				}
			}
		}

		return $typos;
	}



// accepts a string
// returns array of likely single missed character typos
// arrays contain only characters that are valid domain names
	function getMissedCharTypos( $word )
	{
		$word = strtolower( $word );
		$typos = array();
		$length = strlen( $word );
// check each character
		for( $i = 0; $i < $length; $i++ )
		{
			$tempWord = '';
			if( $i == 0 )
			{
// at first character
				$tempWord = substr( $word, ( $i + 1 ) );

			} else if ( ( $i + 1 ) == $length ) {
// at last character
				$tempWord = substr( $word, 0,  $i  ) ;

			} else {
// in between
				$tempWord = substr( $word, 0,  $i  ) ;
				$tempWord .= substr( $word, ( $i + 1 )) ;

			}
			array_push( $typos, $tempWord );
		}

		return $typos;
	}


// accepts a string
// returns array of likely transposed character typos
// arrays contain only characters that are valid domain names
	function getTransposedCharTypos( $word )
	{
		$word = strtolower( $word );
		$typos = array();
		$length = strlen( $word );
// check each character
		for( $i = 0; $i < $length; $i++ )
		{
			if( ( $i + 1 ) == $length )
			{
// could have simplified the test by throwing it in the for loop but I didn't to keep it readable
// at the end no transposition
			} else {
				$tempWord = $word;
				$tempChar = $tempWord{$i};			
				$tempWord{$i} = $tempWord{( $i + 1 )} ;			
				$tempWord{( $i + 1 )} = $tempChar;			
				array_push( $typos, $tempWord );
			}
		}

		return $typos;
	}





// accepts a string
// returns array of likely double entered character typos
// arrays contain only characters that are valid domain names
	function getDoubleCharTypos( $word )
	{
		$word = strtolower( $word );
		$typos = array();
		$length = strlen( $word );
// check each character
		for( $i = 0; $i < $length; $i++ )
		{
// get first part of word
			$tempWord = substr( $word, 0, ($i+1) );
// add a character
			$tempWord .= $word{$i};
// add last part of strin if there is any 
			if( $i == ( $length - 1 ))
			{
// do nothing we are at the end
			} else {
// add the end part of the string
				$tempWord .= substr( $word, ($i+1));
			}
			array_push( $typos, $tempWord );
		}

		return $typos;
	}


}


?>

Re: How to display results PHP ? (Noob Question)

Posted: Sun Jan 23, 2011 2:06 pm
by Jade
Um... it tells you how to use it right at the top of the code you posted.

Code: Select all

Example Usage:
-----------------------------------------------------------
$word = "Hello";
$typos = array();
$typos = cTypoGenerator::getAllTypos( $word );

print_r( $typos );

Re: How to display results PHP ? (Noob Question)

Posted: Sun Jan 23, 2011 2:46 pm
by AutoRunway
I saw that so I figured I'd do something like this but didn't work :(

Code: Select all

<form method="get" action="typo.php">
<h3>Typo Generator</h3><input type="text" name="term" /><input type="submit" /></form>
<?php
$typos = cTypoGenerator::getAllTypos( $word );
?>

Re: How to display results PHP ? (Noob Question)

Posted: Sun Jan 23, 2011 4:20 pm
by Peec

Code: Select all

<form method="get" action="typo.php">
<h3>Typo Generator</h3><input type="text" name="term" /><input type="submit" /></form>
<?php
if (isset($_POST['term'])){
$typos = cTypoGenerator::getAllTypos($_POST['term'] );
print_r($typos);
}
?>


Re: How to display results PHP ? (Noob Question)

Posted: Mon Jan 31, 2011 7:28 am
by Jade
Did you remember to include the file? Also, you need to use POST as your form method if you're using the $_POST variable to retrieve the data. If you use get as your form method then you'd use $_GET['term'];

Code: Select all

<?php
require_once('ctypogenerator_filename_here.php');
?>
<form method="post" action="typo.php">
<h3>Typo Generator</h3><input type="text" name="term" /><input type="submit" /></form>
<?php
if (isset($_POST['term'])){
$typos = cTypoGenerator::getAllTypos($_POST['term'] );
print_r($typos);
}
?>