domingo, 26 de junio de 2016

php simple mysql conection

<?php

$setting=array("username"=>"root","password"=>"123456","host"=>"127.0.0.1","db"=>"dialer");


$link = mysqli_connect($setting["host"],$setting["username"],$setting["password"],$setting["db"]);

$query= "SELECT * from  audio ";


if ($result = mysqli_query($link, $query)) {
    while ($row = mysqli_fetch_assoc($result)) {


echo "$row[audio_id]\n";

}


}


else {



    printf(" failed: %s\n", mysqli_error($link));

}

/* close connection */
mysqli_close($link);


?>

martes, 21 de junio de 2016

substr

substr

(PHP 4, PHP 5, PHP 7)
substr — Return part of a string

Description ¶

string substr ( string $string , int $start [, int $length ] )
Returns the portion of string specified by the start and length parameters.

Parameters ¶

string
The input string. Must be one character or longer.
start
If start is non-negative, the returned string will start at the start'th position in string, counting from zero. For instance, in the string 'abcdef', the character at position 0 is 'a', the character at position 2 is 'c', and so forth.
If start is negative, the returned string will start at the start'th character from the end of string.
If string is less than start characters long, FALSE will be returned.
Example #1 Using a negative start
<?php
echo  substr ("abcde" , 0,3 );
?>
retrun abc




extension state script

<?php
$extension_list=array(100=>100,101=>101,102=>102,103=>103,104=>104,105=>105,106=>106,107=>107,108=>108,109=>109);
foreach($extension_list as $key=> $extension) {

 $output = shell_exec("asterisk -x \" core show hint $extension\" | awk '{print $4}' | awk 'NR==1'");
 $state = substr("$output", -5);
  $state=trim ($state);

if($state=="Idle") {
echo "agent $key avaiable\n";

}


}
?>

Execute command via shell and return the complete output as a variable

shell_exec

(PHP 4, PHP 5, PHP 7)
shell_exec — Execute command via shell and return the complete output as a string

Description ¶

string shell_exec ( string $cmd )
This function is identical to the backtick operator.

Parameters ¶

cmd
The command that will be executed.

Return Values ¶

The output from the executed command or NULL if an error occurred or the command produces no output.
Note:
This function can return NULL both when an error occurs or the program produces no output. It is not possible to detect execution failures using this function. exec() should be used when access to the program exit code is required.

Examples ¶

Example #1 A shell_exec() example
<?php
$output 
shell_exec('ls -lart');
echo 
"<pre>$output</pre>";?>

Notes ¶

Note:
This function is disabled when PHP is running in safe mode.

See Also ¶

add a note add a note

User Contributed Notes

viernes, 17 de junio de 2016

How to Validate Email Address in PHP


So How to Validate Email Address in PHP?

Here I'll show you two different methods to validate email address in php. The first method is by using filter_var() function and the second one by using regex pattern.

Method 1: Using filter_var() function

This is the absolute easiest way by which you can validate an email address in php. The method uses PHP's filter_var() function which is simple and safe to check if an email id is well formed and valid. The only downside to this method is it works only with PHP >= v5.2.0.
Below is the php function to validate email address using filter_var().
<?php
function validate_email($email=NULL) {
    return (filter_var($email, FILTER_VALIDATE_EMAIL) ? "$email is a valid email-id" : "$email is an invalid email-id");
}

echo validate_email("user.example@gmail.com");
echo "<br/>" . validate_email("user.example.com");

// output
// user.example@gmail.com is a valid email-id
// user.example.com is an invalid email-id
?>

Method 2: Using Mighty RegEx

If your PHP version is less than 5.2.0, then you have to go with the (crazy) regular expression method to validate the email address. Here is the php function to check if email-id is valid or not using regular expressions.
<?php
function validate_email($email=NULL) {
    return (preg_match("/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/",$email) ? "$email is a valid email-id" : "$email is an invalid email-id");
}

echo validate_email("user.example@gmail.com");
echo "<br/>" . validate_email("user_example@yahoo");

// output
// user.example@gmail.com is a valid email-id
// user_example@yahoo is an invalid email-id
?>
By using the above two methods you can easily validate the email address in php but still using filter_var() is the best option in most of the cases.
http://www.kodingmadesimple.com/2016/06/how-to-validate-email-address-in-php.html?utm_content=buffer63221&utm_medium=social&utm_source=facebook.com&utm_campaign=buffer