File "test_valeur.php"

Full Path: /home/analogde/www/Prog/MMM/Fusion/WEB/test_valeur.php
File size: 6.13 KB
MIME-type: text/x-php
Charset: utf-8

<?php


	
	/*
	if('string1' == 'string1')
	{
		echo 'Strings match.';
	} 
	else 
	{
		echo 'Strings do not match.';
	}

	if($str == $res)
	{
		echo 'Strings OK';
	} 
	else 
	{
		echo 'Strings error';
	}
	*/
	
	$str = "1235";
	$res = preg_replace("/[^0-9.]/", "", $str );
	echo $res;
	compare($str, $res);
	echo "</br>";

	$str = "aze";
	$res = preg_replace("/[^0-9.]/", "", $str );
	echo $res;
	compare($str, $res);
	echo "</br>";

	$str = "8.15";
	$res = preg_replace("/[^0-9.]/", "", $str );
	echo $res;
	compare($str, $res);
	echo "</br>";

	$str = "AZ9.78";
	$res = preg_replace("/[^0-9.]/", "", $str );
	echo $res;
	compare($str, $res);
	echo "</br>";


	$str = ".127.78.2.5";
	$res = preg_replace("/[^0-9.]/", "", $str );
	echo $res;
	compare($str, $res);
	echo "</br>";

	$str = ".357";
	$res = preg_replace("/[^0-9.]/", "", $str );
	echo $res;
	compare($str, $res);
	echo "</br>";
	echo "</br>";
	
	echo " ------- ";

	echo "</br>";
	echo "</br>";
	$str = ".357";
	$pieces = str_split($str);
	// supprimer le point en premiére position
	if( $pieces[0] == chr(46) )
	{
		$str1 = substr($str, 1); 
		echo $str ." devient " .$str1;
		echo "</br>";
	}		

	echo " ------- ";
	
	echo "</br>";
	echo "</br>";
	$str = "357.";
	$pieces = str_split($str);
	// supprimer le point en derniere position
	if( end($pieces) == chr(46) )
	{
		$str1 = substr($str, 0, -1); 
		echo $str ." devient " .$str1;
		echo "</br>";
	}		

	echo " ------- ";
	
	echo "</br>";
	echo "</br>";

	//plusieurs points dans la chaine

	$str = ".127.78.2.5";
	$pieces = str_split($str);
	
	//.127.78.2.5

	//127.78.2.5
	
	//$array = array(1, "hello", 1, "world", "hello");

	echo " ---> " .$str;
	
	$resultArray = array_count_values($pieces)
	print_r(array_count_values($pieces));
	
	echo "</br>";
	echo "</br>";
	
	for ($x = 0; $x < count($resultArray); $x++) 
	{
		if( $resultArray[$x] == chr(46) )
		{
			echo " ----->" +$resultArray[chr46];
		}
	}
	
	


	// OK

	//echo preg_replace('~\D~', '', $str);

	//echo "</br>";

	//echo "</br>";





	echo test("1235");

	echo "</br>";

	echo test("aze");

	echo "</br>";

	echo test("8.15");

	echo "</br>";

	echo test("AZ9.78");

	echo "</br>";

	echo test(".127.78.2.5");

	echo "</br>";

	echo test(".357");

	echo "</br>";



	echo "</br>";

	echo "</br>";

	echo "</br>";



	$str = "A";

	if( $str == chr(65) )

	{

		echo "Bingo";

		echo "<br>";

	}		



	echo preg_match("/^[a-z0-9.]+$/i", "123");

	echo "<br>";

	echo preg_match("/^[a-z0-9.]+$/i", "aze");

	echo "<br>";

	echo preg_match("/^[a-z0-9.]+$/i", "123.78");

	echo "<br>";

	echo preg_match("/^[a-z0-9.]+$/i", "12a3.Z78");

	echo "<br>";

	echo "<br>";

	

	

	

	

	

	$str = '123454321';

	$pattern = '/^[0-9]+$/';

	if ( preg_match($pattern, $str) ) 

	{

		echo 'string contains only digits';

	} 

	else 

	{

		echo 'string does not contain only digits';

	}

	

	

	/*$pieces = str_split("789123");

	

	for ($i=0; $i<count($pieces); $i++)

	{

		echo $pieces[$i] ."<br>";

		

		if( $str != chr(48) || $str != chr(49) || $str != chr(50) || $str != chr(51) ||

			$str != chr(52) || $str != chr(53) || $str != chr(54) || $str != chr(55) ||

			$str != chr(56) || $str != chr(57)  )

		{

			echo "Probléme";

			echo "<br>";

		}		

	}

	*/

	echo "<br>";

	echo "<br>";

	echo "<br>";



	echo hasNumbers("123");

	echo "<br>";

	echo hasNumbers("aze");

	echo "<br>";

	echo hasNumbers("0a8kp");

	echo "<br>";



	$string = "789";



	if (preg_match('~[0-9]+~', $string)) 

	{

		echo 'string with numbers';

		echo "<br>";

	}



	$string = "7.89";



	if (preg_match('~[0-9]+~', $string)) 

	{

		echo 'string with numbers';

		echo "<br>";

	}



	$string = "a8";



	if (preg_match('~[0-9]+~', $string)) 

	{

		echo 'string with numbers';

		echo "<br>";

	}

	

	$string = "agdf";



	if (preg_match('~[0-9]+~', $string)) 

	{

		echo 'string with numbers';

		echo "<br>";

	}



	//echo intval("42");           



	//echo "<br>";



	//echo intval("AZ"); 

	

	//echo "<br>";



	//echo intval("10.57"); 

	

	$error = "78.7.21";

	

	$tests = array(

    "42",

    1337,

    0x539,

    02471,

    0b10100111001,

    1337e0,

    "0x539",

    "02471",

    "0b10100111001",

    "1337e0",

    "not numeric",

    array(),

    9.1,

	$error,

    null,

    '',

);



foreach ($tests as $element) {

    if (is_numeric($element)) {

        echo var_export($element, true) . " is numeric", PHP_EOL;

    } else {

        echo var_export($element, true) . " is NOT numeric", PHP_EOL;

    }

	

	echo "<br>";

}

function compare($string1, $string2)
{
	if (strcmp($string1, $string2) == 0) 
	//if ( $string1 == $string2)  
	{
		echo 'Equal';
	}
	else
	{
		echo 'Differente';
	}
}	


function hasNumbers($strValue)

{

    return preg_match('/[0-9]/', $strValue); // === 1;

}	



function test($val)

{

	$pieces = str_split($val);

	

	//echo "<pre>";

	//print_r($pieces);

	//echo "</pre>";

	

	$count_point = substr_count($val, '.');

	

	echo "  ----> Nb points " .$count_point ."  +++++++  ";

	echo "</br>";

	

	/*if( $pieces[0] == chr(46) )

	{

		$detect_error = true;

		echo " TRACE1 ";



	}*/		

	

	//else

	//{

		//echo " TRACE2 ";

	

		

		if( $count_point < 2 ) // pas de point ou 1

		{

			$detect_error = false;

			

			for ($i=0; $i<count($pieces); $i++)

			{

				echo "---> $i " .$pieces[$i] ."<br>";

				if( $pieces[$i] == chr(46)   )

				{

					$detect_error = false;

					if( $i == 0 ) // erreur si on commence par un point 

					{

						$detect_error = true;

						break;

					}						

				}

				else if( $pieces[$i] >= chr(48) && $pieces[$i] <= chr(57)  )

				{

					$detect_error = false;

				}	

				else

				{

					$detect_error = true;

					break;

				}			

			}

		}

		else 

		{

			$detect_error = true;

			//$detect_error = "lulu";

			//echo "fdgljqjglkfjgklfjqljqjsdl     "  .$detect_error ;

		}		

	//}

	

	

	

	if( $detect_error )

	{

		$retour = "Erreur";

	}

	else

	{

		$retour = "OK";

	}		

	return $retour ." " .$val;

	

}



?>