<?php
$multi=array("a"=>1,"b"=>2,"c"=>array("d"=>array("e"=>500)));
echo $multi['c']['d']['e'];
echo "<br>";
foreach( $multi['c']['d'] as $key=> $value){
echo " Key is $key Value is $value <br>";
}
?>
miércoles, 30 de diciembre de 2015
Accessing array elements
Example #6 Accessing array elements
<?php
$array = array(
"foo" => "bar",
42 => 24,
"multi" => array(
"dimensional" => array(
"array" => "foo"
)
)
);
var_dump($array["foo"]);var_dump($array[42]);var_dump($array["multi"]["dimensional"]["array"]);?>
string(3) "bar"
int(24)
string(3) "foo"
sábado, 26 de diciembre de 2015
php mysql function
list.php
<?php
require_once("func.php");
require_once("setting.php");
$titles=Array("ID","Name","Path","Desc");
$record=array("list_field_id","_name","phone","call_status");
$query="select * from list";
$db_con=conect_db($setting[host],$setting[username],$setting[password],$setting[db]);
table($titles);
display_records($db_con,$query,$record);
?>
setting.php
<?php
/* db conection settings */
$setting=array("username"=>"root","password"=>"12340","host"=>"127.0.0.1","db"=>"dialer");
?>
func.php
<?php
/*db conection function */
function conect_db($host,$username,$password,$db) {
$link = mysqli_connect("$host", "$username", "$password", "$db");
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
else {
echo " Sucessfully conected to the database $db ";
}
return $link;
}
/*table header function */
function table($titles){
echo"<br>
<center>
<table width=\"500\" border=\"0\" align=\"center\" cellpadding=\"3\" cellspacing=\"1\" bgcolor=\"#999999\" onMouseOver=this.bgColor=\"gold\" onMouseOut=this.bgColor=\"#CCCCCC\">
<tr align=\"center\" bgcolor=\"#CCCCCC\">";
foreach($titles as $value){
echo "<td width=\"150\" nowrap=\"nowrap\"><strong>$value</strong></td>";
}
}
/*db display db elemtn function */
function display_records($link,$query,$record) {
if ($result = mysqli_query($link,$query)) {
$row_cnt = mysqli_num_rows($result);
printf("Result set has %d rows.\n", $row_cnt);
while ($row = mysqli_fetch_assoc($result)) {
echo '<tr bgcolor="#FFFFFF" onMouseOver=this.bgColor="gold" onMouseOut=this.bgColor="white">';
foreach($record as $value) {
}
}
else {
printf(" Query failed: %s\n", mysqli_error($link));
exit();
}
}
?>
<?php
require_once("func.php");
require_once("setting.php");
$titles=Array("ID","Name","Path","Desc");
$record=array("list_field_id","_name","phone","call_status");
$query="select * from list";
$db_con=conect_db($setting[host],$setting[username],$setting[password],$setting[db]);
table($titles);
display_records($db_con,$query,$record);
?>
setting.php
<?php
/* db conection settings */
$setting=array("username"=>"root","password"=>"12340","host"=>"127.0.0.1","db"=>"dialer");
?>
func.php
<?php
/*db conection function */
function conect_db($host,$username,$password,$db) {
$link = mysqli_connect("$host", "$username", "$password", "$db");
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
else {
echo " Sucessfully conected to the database $db ";
}
return $link;
}
/*table header function */
function table($titles){
echo"<br>
<center>
<table width=\"500\" border=\"0\" align=\"center\" cellpadding=\"3\" cellspacing=\"1\" bgcolor=\"#999999\" onMouseOver=this.bgColor=\"gold\" onMouseOut=this.bgColor=\"#CCCCCC\">
<tr align=\"center\" bgcolor=\"#CCCCCC\">";
foreach($titles as $value){
echo "<td width=\"150\" nowrap=\"nowrap\"><strong>$value</strong></td>";
}
}
/*db display db elemtn function */
function display_records($link,$query,$record) {
if ($result = mysqli_query($link,$query)) {
$row_cnt = mysqli_num_rows($result);
printf("Result set has %d rows.\n", $row_cnt);
while ($row = mysqli_fetch_assoc($result)) {
echo '<tr bgcolor="#FFFFFF" onMouseOver=this.bgColor="gold" onMouseOut=this.bgColor="white">';
foreach($record as $value) {
echo "<td width=\"150\" nowrap=\"nowrap\">" .$row[$value] . "</td>";
}}
}
else {
printf(" Query failed: %s\n", mysqli_error($link));
exit();
}
}
?>
viernes, 25 de diciembre de 2015
php username checker function
<?php
$username=array("admin","root","guest");
function username_check($uname,$user_list) {
if(in_array($uname,$user_list)){
echo " username is already selected";
exit();
}
else{
echo " access granted<br>";
}
}
username_check("root1",$username);
echo <<<WELCOME
<b> Welcome to my website</b>
WELCOME;
?>
$username=array("admin","root","guest");
function username_check($uname,$user_list) {
if(in_array($uname,$user_list)){
echo " username is already selected";
exit();
}
else{
echo " access granted<br>";
}
}
username_check("root1",$username);
echo <<<WELCOME
<b> Welcome to my website</b>
WELCOME;
?>
jueves, 24 de diciembre de 2015
mysqli_multi_query
<?php
$link = mysqli_connect("localhost", "my_user", "my_password", "world");
/* check connection */if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$query = "SELECT CURRENT_USER();";$query .= "SELECT Name FROM City ORDER BY ID LIMIT 20, 5";
/* execute multi query */if (mysqli_multi_query($link, $query)) {
do {
/* store first result set */
if ($result = mysqli_store_result($link)) {
while ($row = mysqli_fetch_row($result)) {
printf("%s\n", $row[0]);
}
mysqli_free_result($result);
}
/* print divider */
if (mysqli_more_results($link)) {
printf("-----------------\n");
}
} while (mysqli_next_result($link));
}
/* close connection */mysqli_close($link);?>
$link = mysqli_connect("localhost", "my_user", "my_password", "world");
/* check connection */if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$query = "SELECT CURRENT_USER();";$query .= "SELECT Name FROM City ORDER BY ID LIMIT 20, 5";
/* execute multi query */if (mysqli_multi_query($link, $query)) {
do {
/* store first result set */
if ($result = mysqli_store_result($link)) {
while ($row = mysqli_fetch_row($result)) {
printf("%s\n", $row[0]);
}
mysqli_free_result($result);
}
/* print divider */
if (mysqli_more_results($link)) {
printf("-----------------\n");
}
} while (mysqli_next_result($link));
}
/* close connection */mysqli_close($link);?>
domingo, 20 de diciembre de 2015
Sending MYSQL table to email
Total of extensions 2
| Extension | Total calls | Duration | Caller ID | ACTION |
| 102 | 2 | 7 Seconds | "102" <102> | |
| 109 | 6 | 15 Seconds | "" <109> |
Sending this table to an email address in with the htm tags, first we load the table on a variable
file_get_contents('http://65.181.118.232/php-project/counter.php');
65.181.118.232/php-project/counter.php i s the url where the table is showed
<?php
$homepage = file_get_contents('http://65.181.118.232/php-project/counter.php');
$body= $homepage;
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
mail("ambiorixg12@gmail.com","Alert",$body,$headers);
echo "message sent";
?>
cleaning string
<?php
$var2="RemoteAddress: IPV4/UDP/74.208.164.60/5068";
$var2=explode("/",$var2);
foreach($var2 as $key=>$value) {
echo "$key $value<br>";
}
?>
$var2="RemoteAddress: IPV4/UDP/74.208.164.60/5068";
$var2=explode("/",$var2);
foreach($var2 as $key=>$value) {
echo "$key $value<br>";
}
?>
sábado, 19 de diciembre de 2015
php sending email with linux mail function
system("echo \"$subject\" | mail -s \"$body\" ambiorixg12@gmail.com");
Function preg_replace() replace and regular expresion
<?php $copy_date = "Copyright 1999"; $copy_date = preg_replace("([0-9]+)", "2000", $copy_date); print $copy_date; ?>
This will produce the following result −
Copyright 2000
domingo, 29 de noviembre de 2015
clean this code
<?php
$str = <<<EOD
Event: MusicOnHoldStop
Privilege: call,all
Channel: SIP/callcentric-0000007c
ChannelState: 6
ChannelStateDesc: Up
CallerIDNum: 0411
CallerIDName: <unknown>
ConnectedLineNum: 2066013560
ConnectedLineName: 109
AccountCode:
Context: recording
Exten:
Priority: 1
Uniqueid: 1448837557.211
EOD;
//echo $str;
$result = stristr($str, 'event');
$a=str_replace(" ","<br>","$result");
$b=stristr($str, 'event');
echo "<br>";
echo "$b";
$c=explode(" ",$b);
echo "<br><br>";
echo $c[0];
?>
$str = <<<EOD
Event: MusicOnHoldStop
Privilege: call,all
Channel: SIP/callcentric-0000007c
ChannelState: 6
ChannelStateDesc: Up
CallerIDNum: 0411
CallerIDName: <unknown>
ConnectedLineNum: 2066013560
ConnectedLineName: 109
AccountCode:
Context: recording
Exten:
Priority: 1
Uniqueid: 1448837557.211
EOD;
//echo $str;
$result = stristr($str, 'event');
$a=str_replace(" ","<br>","$result");
$b=stristr($str, 'event');
echo "<br>";
echo "$b";
$c=explode(" ",$b);
echo "<br><br>";
echo $c[0];
?>
finding if string exist strripos()
Example #1 A simple strripos() example
<?php
$haystack = 'ababcd';
$needle = 'aB';
$pos = strripos($haystack, $needle);
if ($pos === false) {
echo "Sorry, we did not find ($needle) in ($haystack)";
} else {
echo "Congratulations!\n";
echo "We found the last ($needle) in ($haystack) at position ($pos)";
}
?>
The above example will output:
Congratulations!
We found the last (aB) in (ababcd) at position (2
<?php
$haystack = 'ababcd';
$needle = 'aB';
$pos = strripos($haystack, $needle);
if ($pos === false) {
echo "Sorry, we did not find ($needle) in ($haystack)";
} else {
echo "Congratulations!\n";
echo "We found the last ($needle) in ($haystack) at position ($pos)";
}
?>
The above example will output:
Congratulations!
We found the last (aB) in (ababcd) at position (2
php searching string using stristr() & strpbrk()
<?php
$email = 'name@example.com';
$domain = stristr($email, '@');
echo $domain."<br>"; // prints @example.com
$user = stristr($email, '@', true); // As of PHP 5.3.0
echo $user;
echo "<br>";
echo strpbrk($email,'a');
?>
@example.com
name
ame@example.com
$email = 'name@example.com';
$domain = stristr($email, '@');
echo $domain."<br>"; // prints @example.com
$user = stristr($email, '@', true); // As of PHP 5.3.0
echo $user;
echo "<br>";
echo strpbrk($email,'a');
?>
@example.com
name
ame@example.com
finding if string exist on string strpos()
<?php
$mystring = 'abc';
$findme = 'a';
$pos = strpos($mystring, $findme);
// The !== operator can also be used. Using != would not work as expected
// because the position of 'a' is 0. The statement (0 != false) uates
// to false.
if ($pos !== false) {
echo "The string '$findme' was found in the string '$mystring'";
echo " and exists at position $pos";
} else {
echo "The string '$findme' was not found in the string '$mystring'";
}
$mystring = 'abc';
$findme = 'a';
$pos = strpos($mystring, $findme);
// The !== operator can also be used. Using != would not work as expected
// because the position of 'a' is 0. The statement (0 != false) uates
// to false.
if ($pos !== false) {
echo "The string '$findme' was found in the string '$mystring'";
echo " and exists at position $pos";
} else {
echo "The string '$findme' was not found in the string '$mystring'";
}
?>
php explode and list() function
<?php
$data = "foo:*:1023:1000::/home/foo:/bin/sh";
list($user, $pass, $uid, $gid, $gecos, $home, $shell) = explode(":", $data);
echo $user; // foo
echo $pass; // *
?>
<?php
$data = "foo:*:1023:1000::/home/foo:/bin/sh";
$r=list($user, $pass, $uid, $gid, $gecos, $home, $shell) = explode(":", $data);
foreach($r as $value ){
echo $value."<br>";
}
?>
outout :
foo
*
1023
1000
/home/foo
/bin/sh
$data = "foo:*:1023:1000::/home/foo:/bin/sh";
list($user, $pass, $uid, $gid, $gecos, $home, $shell) = explode(":", $data);
echo $user; // foo
echo $pass; // *
?>
<?php
$data = "foo:*:1023:1000::/home/foo:/bin/sh";
$r=list($user, $pass, $uid, $gid, $gecos, $home, $shell) = explode(":", $data);
foreach($r as $value ){
echo $value."<br>";
}
?>
outout :
foo
*
1023
1000
/home/foo
/bin/sh
sábado, 7 de noviembre de 2015
php date() format
<?php
// Assuming today is March 10th, 2001, 5:16:18 pm, and that we are in the
// Mountain Standard Time (MST) Time Zone
$today = date("F j, Y, g:i a"); // March 10, 2001, 5:16 pm
$today = date("m.d.y"); // 03.10.01
$today = date("j, n, Y"); // 10, 3, 2001
$today = date("Ymd"); // 20010310
$today = date('h-i-s, j-m-y, it is w Day'); // 05-16-18, 10-03-01, 1631 1618 6 Satpm01
$today = date('\i\t \i\s \t\h\e jS \d\a\y.'); // it is the 10th day.
$today = date("D M j G:i:s T Y"); // Sat Mar 10 17:16:18 MST 2001
$today = date('H:m:s \m \i\s\ \m\o\n\t\h'); // 17:03:18 m is month
$today = date("H:i:s"); // 17:16:18
$today = date("Y-m-d H:i:s"); // 2001-03-10 17:16:18 (the MySQL DATETIME format)
?>
http://php.net/manual/en/function.date.php
// Assuming today is March 10th, 2001, 5:16:18 pm, and that we are in the
// Mountain Standard Time (MST) Time Zone
$today = date("F j, Y, g:i a"); // March 10, 2001, 5:16 pm
$today = date("m.d.y"); // 03.10.01
$today = date("j, n, Y"); // 10, 3, 2001
$today = date("Ymd"); // 20010310
$today = date('h-i-s, j-m-y, it is w Day'); // 05-16-18, 10-03-01, 1631 1618 6 Satpm01
$today = date('\i\t \i\s \t\h\e jS \d\a\y.'); // it is the 10th day.
$today = date("D M j G:i:s T Y"); // Sat Mar 10 17:16:18 MST 2001
$today = date('H:m:s \m \i\s\ \m\o\n\t\h'); // 17:03:18 m is month
$today = date("H:i:s"); // 17:16:18
$today = date("Y-m-d H:i:s"); // 2001-03-10 17:16:18 (the MySQL DATETIME format)
?>
http://php.net/manual/en/function.date.php
calculating the minute difference between dates
<?php
$datetime="2015-11-07 23:20:03";
$datetime1 = strtotime($datetime ); //convert date to unixtime
$datetime2 = time(); //actual date on unixtime
$interval = abs($datetime2 - $datetime1);
$minutes = round($interval / 60);
return $minutes;
?>
$datetime="2015-11-07 23:20:03";
$datetime1 = strtotime($datetime ); //convert date to unixtime
$datetime2 = time(); //actual date on unixtime
$interval = abs($datetime2 - $datetime1);
$minutes = round($interval / 60);
return $minutes;
?>
check and uncheck
<!DOCTYPE html>
<html>
<head>
<script>
function togglecheckboxes(master,group){
var cbarray = document.getElementsByClassName(group);
for(var i = 0; i < cbarray.length; i++){
var cb = document.getElementById(cbarray[i].id);
cb.checked = master.checked;
}
}
</script>
</head>
<body>
<form action="show_all.php" method=POST>
<input type="checkbox" id="cbgroup1_master" onchange="togglecheckboxes(this,'cbgroup1')"> Toggle All
<br><br>
<input type="checkbox" id="cb1_1" class="cbgroup1" name="cbg1[]" value="1"> Item 1<br>
<input type="checkbox" id="cb1_2" class="cbgroup1" name="cbg1[]" value="2"> Item 2<br>
<input type="checkbox" id="cb1_3" class="cbgroup1" name="cbg1[]" value="3"> Item 3<br>
<input type="checkbox" id="cb1_4" class="cbgroup1" name="cbg1[]" value="4"> Item 4<br>
<input type="submit" value="Submit">
</form>
</body>
</html>
PART 2
<?php
foreach ($_POST[cbg1] as $d )
{
echo $d."\n";
}
echo $_GET['id'];
?>
<html>
<head>
<script>
function togglecheckboxes(master,group){
var cbarray = document.getElementsByClassName(group);
for(var i = 0; i < cbarray.length; i++){
var cb = document.getElementById(cbarray[i].id);
cb.checked = master.checked;
}
}
</script>
</head>
<body>
<form action="show_all.php" method=POST>
<input type="checkbox" id="cbgroup1_master" onchange="togglecheckboxes(this,'cbgroup1')"> Toggle All
<br><br>
<input type="checkbox" id="cb1_1" class="cbgroup1" name="cbg1[]" value="1"> Item 1<br>
<input type="checkbox" id="cb1_2" class="cbgroup1" name="cbg1[]" value="2"> Item 2<br>
<input type="checkbox" id="cb1_3" class="cbgroup1" name="cbg1[]" value="3"> Item 3<br>
<input type="checkbox" id="cb1_4" class="cbgroup1" name="cbg1[]" value="4"> Item 4<br>
<input type="submit" value="Submit">
</form>
</body>
</html>
PART 2
<?php
foreach ($_POST[cbg1] as $d )
{
echo $d."\n";
}
echo $_GET['id'];
?>
sábado, 31 de octubre de 2015
checking if a word exist on an string
<?php
// check the if the word Ringing exist on the $result string
if(strpos($result,'Ringing')) {
echo "Agent Station Rining $result<br><br>";
}
// check the if the word Ringing exist on the $result string
if(strpos($result,'Ringing')) {
echo "Agent Station Rining $result<br><br>";
}
?>
http://stackoverflow.com/questions/4366730/check-if-string-contains-specific-words
PHP counting words in an specify string
Use a combination of
str_word_count() and array_count_values():$str = 'happy beautiful happy lines pear gin happy lines rock happy lines pear ';
$words = array_count_values(str_word_count($str, 1));
print_r($words);
gives
Array
(
[happy] => 4
[beautiful] => 1
[lines] => 3
[pear] => 2
[gin] => 1
[rock] => 1
)
The
1 in str_word_count() makes the function return an array of all the found words.
To sort the entries, use
arsort() (it preserves keys):arsort($words);
print_r($words);
Array
(
[happy] => 4
[lines] => 3
[pear] => 2
[rock] => 1
[gin] => 1
[beautiful] => 1
)
http://stackoverflow.com/questions/2984786/php-sort-and-count-instances-of-words-in-a-given-string
miércoles, 28 de octubre de 2015
PHP FORM validation
<?phpif(isset($_POST['email'])) { // CHANGE THE TWO LINES BELOW $email_to = "you@yourdomain.com"; $email_subject = "website html form submissions"; function died($error) { // your error code can go here echo "We are very sorry, but there were error(s) found with the form you submitted. "; echo "These errors appear below.<br /><br />"; echo $error."<br /><br />"; echo "Please go back and fix these errors.<br /><br />"; die(); } // validation expected data exists if(!isset($_POST['first_name']) || !isset($_POST['last_name']) || !isset($_POST['email']) || !isset($_POST['telephone']) || !isset($_POST['comments'])) { died('We are sorry, but there appears to be a problem with the form you submitted.'); } $first_name = $_POST['first_name']; // required $last_name = $_POST['last_name']; // required $email_from = $_POST['email']; // required $telephone = $_POST['telephone']; // not required $comments = $_POST['comments']; // required $error_message = ""; $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/'; if(!preg_match($email_exp,$email_from)) { $error_message .= 'The Email Address you entered does not appear to be valid.<br />'; } $string_exp = "/^[A-Za-z .'-]+$/"; if(!preg_match($string_exp,$first_name)) { $error_message .= 'The First Name you entered does not appear to be valid.<br />'; } if(!preg_match($string_exp,$last_name)) { $error_message .= 'The Last Name you entered does not appear to be valid.<br />'; } if(strlen($comments) < 2) { $error_message .= 'The Comments you entered do not appear to be valid.<br />'; } if(strlen($error_message) > 0) { died($error_message); } $email_message = "Form details below.\n\n"; function clean_string($string) { $bad = array("content-type","bcc:","to:","cc:","href"); return str_replace($bad,"",$string); } $email_message .= "First Name: ".clean_string($first_name)."\n"; $email_message .= "Last Name: ".clean_string($last_name)."\n"; $email_message .= "Email: ".clean_string($email_from)."\n"; $email_message .= "Telephone: ".clean_string($telephone)."\n"; $email_message .= "Comments: ".clean_string($comments)."\n"; // create email headers$headers = 'From: '.$email_from."\r\n".'Reply-To: '.$email_from."\r\n" .'X-Mailer: PHP/' . phpversion();@mail($email_to, $email_subject, $email_message, $headers); ?><!-- place your own success html below -->Thank you for contacting us. We will be in touch with you very soon.<?php}die();?>
Suscribirse a:
Comentarios (Atom)