return devuelve el valor de una variable x llamada dentro de una funcion, y transforma la funcion misma en una variable misma igual al valor de la variable x ya mencionada con el return
ejemplo $a sera igual al valor de la suma de $var+$var1 y la funcion sum( $var,$var1) sera igual al valor de $a. Por ende podemos usar esta funcion como si fuese una variable
<?
function sum($var,$var1){
$a=$var+$var1;
return $a;
}
if(sum(10,10)==20){
echo " function value is 20";
}
else {
echo " function value is not equal to 20";
}
?>
function value is 20
viernes, 27 de febrero de 2015
martes, 24 de febrero de 2015
using-linux-awk-command-in-php
$output = exec('awk -v OFS=, \'/hello/i {$1=$1;print}\' test.txt > newtest.txt');
http://stackoverflow.com/questions/16419070/using-linux-awk-command-in-php
PHP Function preg_split()
Advertisements
Syntax
array preg_split (string pattern, string string [, int limit [, int flags]]); |
Definition and Usage
The preg_split() function operates exactly like split(), except that regular expressions are accepted as input parameters for pattern.If the optional input parameter limit is specified, then only limit number of substrings are returned.
flags can be any combination of the following flags:
- PREG_SPLIT_NO_EMPTY: If this flag is set, only non-empty pieces will be returned by preg_split().
- PREG_SPLIT_DELIM_CAPTURE: If this flag is set, parenthesized expression in the delimiter pattern will be captured and returned as well.
- PREG_SPLIT_OFFSET_CAPTURE: If this flag is set, for every occurring match the appendant string offset will also be returned.
Return Value
- Returns an array of strings after splitting up a string.
Example
Following is the piece of code, copy and paste this code into a file and verify the result.<?php $ip = "123.456.789.000"; // some IP address $iparr = split ("/\./", $ip); print "$iparr[0] <br />"; print "$iparr[1] <br />" ; print "$iparr[2] <br />" ; print "$iparr[3] <br />" ; ?> |
123 456 789 000 |
domingo, 22 de febrero de 2015
php basic function return
<?php
function sum($a,$b){
$x=$a+$b;
return $x;
}
$r= sum(411,45);
echo $r;
////////
salida
456
--------------------
<?php
function sum($a,$b){
echo ($a+$b);
}
$r= sum(411,45)
////salida 456
------------------------
<?php
function sum($a,$b){
return ($a+$b);
}
$r= sum(411,45);
echo $r;
////salida 456
//////////COLOR OUPUT
<?php
function alert($var) {
if ($var<50) {
$var="<font color=blue>$var</font>";
}
if($var>50) {
$var="<font color=red>$var</font>";
}
return $var;
}
echo '$var color is '.alert(30);
?>
function sum($a,$b){
$x=$a+$b;
return $x;
}
$r= sum(411,45);
echo $r;
////////
salida
456
--------------------
<?php
function sum($a,$b){
echo ($a+$b);
}
$r= sum(411,45)
////salida 456
------------------------
<?php
function sum($a,$b){
return ($a+$b);
}
$r= sum(411,45);
echo $r;
////salida 456
//////////COLOR OUPUT
<?php
function alert($var) {
if ($var<50) {
$var="<font color=blue>$var</font>";
}
if($var>50) {
$var="<font color=red>$var</font>";
}
return $var;
}
echo '$var color is '.alert(30);
?>
Fatal error: Call to undefined function mysql_connect()
Open your terminal and run bellow command.
sudo apt-get install mysql-server
If you are running PHP you will also need to install the php module for mysql 5:sudo apt-get install php5-mysql
http://stackoverflow.com/questions/10615436/fatal-error-call-to-undefined-function-mysql-connect
jueves, 19 de febrero de 2015
PHP logical operators and or
$a=10;
$b=11;
if($a==10 and $b==11)
{
echo $a+$b;
}
else {
echo $a-$b;
}
21
------------
$a=10;
$b=111;
if($a==10 or $b==11)
{
echo $a+$b;
}
else {
echo $a-$b;
}
121
$b=11;
if($a==10 and $b==11)
{
echo $a+$b;
}
else {
echo $a-$b;
}
21
------------
$a=10;
$b=111;
if($a==10 or $b==11)
{
echo $a+$b;
}
else {
echo $a-$b;
}
121
while and break and continue
$i = 1;
while ($i <= 10) {
echo $i++."<br>";
if ($i==6)
{
echo "founded $i";
break;
}
}
1
2
3
4
5
founded 6
------------
$i = 1;
while ($i <= 10) {
echo $i++."<br>";
if ($i==6)
{
echo "founded $i";
continue;
}
}
1
2
3
4
5
founded 66
7
8
9
10
while ($i <= 10) {
echo $i++."<br>";
if ($i==6)
{
echo "founded $i";
break;
}
}
1
2
3
4
5
founded 6
------------
$i = 1;
while ($i <= 10) {
echo $i++."<br>";
if ($i==6)
{
echo "founded $i";
continue;
}
}
1
2
3
4
5
founded 66
7
8
9
10
php elseif
<?phpif($a) { conditional1(); }
elseif($b) { conditional2(); }
elseif($c) { conditional3(); }
elseif($d) { conditional4(); }
elseif($e) { conditional5(); }
elseif($f) { conditional6(); }
elseif($g) { conditional7(); }
elseif($h) { conditional8(); }
else { conditional9(); }?>
lunes, 16 de febrero de 2015
Query por fecaha actual
mysql> select count(*) as calls,dcontext as ivr,month(now()) as month ,dayofmonth(now())as day from cdr where dcontext='ivr_5' and month(calldate)=month(now()) and dayofmonth(calldate)=dayofmonth(now());
+-------+-----+-------+------+
| calls | ivr | month | day |
+-------+-----+-------+------+
| 0 | NULL | 2 | 16 |
+-------+-----+-------+------+
1 row in set (0.16 sec)
calldate es dnde estan las fechas
+-------+-----+-------+------+
| calls | ivr | month | day |
+-------+-----+-------+------+
| 0 | NULL | 2 | 16 |
+-------+-----+-------+------+
1 row in set (0.16 sec)
calldate es dnde estan las fechas
lunes, 9 de febrero de 2015
Routing Script
<html>
<head>
<title>Update a Record in MySQL Database</title>
</head>
<body>
<?php
if(isset($_POST['update']))
{
$dbhost = 'localhost:3036';
$dbuser = 'root';
$dbpass = '123';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
die('Could not connect: ' . mysql_error());
}
$emp_id = $_POST['emp_id'];
$emp_salary = $_POST['emp_salary'];
$sql= "INSERT INTO `asterisk`.`pbx` (`pbx_id`, `ip`, `status`, `date`, `name`, `did`, `reserved2`) VALUES (NULL, '$_POST[ip]', 'enabled', CURRENT_TIMESTAMP, '$_POST[name]', '$_POST[did]', '1');";
mysql_select_db('asterisk');
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
die('Could not update data: ' . mysql_error());
}
header("Location:routing.php");
echo "Updated data successfully\n";
mysql_close($conn);
}
else
{
}
?>
<form method="post" action="<?php $_PHP_SELF ?>">
<table width="400" border="0" cellspacing="1" cellpadding="2">
<tr>
<td width="100"> DID NUMBER</td>
<td><input name="did" type="text" id="did"></td>
</tr>
<tr>
<td width="100"> DNS/IP</td>
<td><input name="ip" type="text" id="ip"></td>
</tr>
<tr>
<td width="100">Trunk Name</td>
<td><input name="name" type="text" id="name"></td>
</tr>
<tr>
<td width="100"> </td>
<td> </td>
</tr>
<tr>
<td width="100"> </td>
<td>
<input name="update" type="submit" id="update" value="Update">
</td>
</tr>
</table>
</form>
<?php
}
?>
</body>
</html>
<?php
$dbhost='localhost';
$dbuser='root';
$dbpass=123;
$dbname='asterisk';
$db = mysql_connect($dbhost, $dbuser, $dbpass) or die("Couldn't connect to the database.");
mysql_select_db($dbname) or die("Couldn't select the database");
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(!$conn )
{
die('Could not connect: ' . mysql_error());
}
$result = mysql_query("SELECT * FROM `pbx` order by date desc");
echo "<strong>Total of records</strong> ".mysql_num_rows($result );
echo "<table border='1' align='center'>
<tr>
<th>ID</th>
<th>DID</th>
<th>DNS/IP</th>
<th>TRUNK</th>
<th>STATUS</th>
<th>DELETE</th>
<th>DATE</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['pbx_id'] . "</td>";
echo "<td>" . $row['did'] . "</td>";
echo "<td>" . $row['ip'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td><a href=?ID=$row[pbx_id]&status=$row[status]>" ."$row[status]" . "</td></a>";
echo "<td><a href=?ID=$row[pbx_id]&del=1>" ."DELETE" . "</td></a>";
echo "<td>" . $row['date'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
<?php
$dbhost='localhost';
$dbuser='root';
$dbpass=123;
$dbname='asterisk';
$db = mysql_connect($dbhost, $dbuser, $dbpass) or die("Couldn't connect to the database.");
if($_GET[ID]>0){
if($_GET[status]=='enabled'){
$status='disabled';
}
if($_GET[status]=='disabled'){
$status='enabled';
}
$query = mysql_query("update `pbx` set status='$status' WHERE `pbx_id`= '$_GET[ID]'");
//UPDATE `asterisk`.`pbx` SET `status` = 'enabled' WHERE `pbx`.`pbx_id` = 22;
if ($query) {
header("Location:routing.php");
}
if (!$query) {
die('Could not query:' . mysql_error());
}
}
?>
<?php
$dbhost='localhost';
$dbuser='root';
$dbpass=123;
$dbname='asterisk';
$db = mysql_connect($dbhost, $dbuser, $dbpass) or die("Couldn't connect to the database.");
if($_GET[ID]>0&& $_GET[del]==1){
$query = mysql_query("DELETE FROM `pbx` WHERE `pbx_id`= '$_GET[ID]'");
if ($query) {
header("Location:routing.php");
}
if (!$query) {
die('Could not query:' . mysql_error());
}
}
?>
<head>
<title>Update a Record in MySQL Database</title>
</head>
<body>
<?php
if(isset($_POST['update']))
{
$dbhost = 'localhost:3036';
$dbuser = 'root';
$dbpass = '123';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
die('Could not connect: ' . mysql_error());
}
$emp_id = $_POST['emp_id'];
$emp_salary = $_POST['emp_salary'];
$sql= "INSERT INTO `asterisk`.`pbx` (`pbx_id`, `ip`, `status`, `date`, `name`, `did`, `reserved2`) VALUES (NULL, '$_POST[ip]', 'enabled', CURRENT_TIMESTAMP, '$_POST[name]', '$_POST[did]', '1');";
mysql_select_db('asterisk');
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
die('Could not update data: ' . mysql_error());
}
header("Location:routing.php");
echo "Updated data successfully\n";
mysql_close($conn);
}
else
{
}
?>
<form method="post" action="<?php $_PHP_SELF ?>">
<table width="400" border="0" cellspacing="1" cellpadding="2">
<tr>
<td width="100"> DID NUMBER</td>
<td><input name="did" type="text" id="did"></td>
</tr>
<tr>
<td width="100"> DNS/IP</td>
<td><input name="ip" type="text" id="ip"></td>
</tr>
<tr>
<td width="100">Trunk Name</td>
<td><input name="name" type="text" id="name"></td>
</tr>
<tr>
<td width="100"> </td>
<td> </td>
</tr>
<tr>
<td width="100"> </td>
<td>
<input name="update" type="submit" id="update" value="Update">
</td>
</tr>
</table>
</form>
<?php
}
?>
</body>
</html>
<?php
$dbhost='localhost';
$dbuser='root';
$dbpass=123;
$dbname='asterisk';
$db = mysql_connect($dbhost, $dbuser, $dbpass) or die("Couldn't connect to the database.");
mysql_select_db($dbname) or die("Couldn't select the database");
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(!$conn )
{
die('Could not connect: ' . mysql_error());
}
$result = mysql_query("SELECT * FROM `pbx` order by date desc");
echo "<strong>Total of records</strong> ".mysql_num_rows($result );
echo "<table border='1' align='center'>
<tr>
<th>ID</th>
<th>DID</th>
<th>DNS/IP</th>
<th>TRUNK</th>
<th>STATUS</th>
<th>DELETE</th>
<th>DATE</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['pbx_id'] . "</td>";
echo "<td>" . $row['did'] . "</td>";
echo "<td>" . $row['ip'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td><a href=?ID=$row[pbx_id]&status=$row[status]>" ."$row[status]" . "</td></a>";
echo "<td><a href=?ID=$row[pbx_id]&del=1>" ."DELETE" . "</td></a>";
echo "<td>" . $row['date'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
<?php
$dbhost='localhost';
$dbuser='root';
$dbpass=123;
$dbname='asterisk';
$db = mysql_connect($dbhost, $dbuser, $dbpass) or die("Couldn't connect to the database.");
if($_GET[ID]>0){
if($_GET[status]=='enabled'){
$status='disabled';
}
if($_GET[status]=='disabled'){
$status='enabled';
}
$query = mysql_query("update `pbx` set status='$status' WHERE `pbx_id`= '$_GET[ID]'");
//UPDATE `asterisk`.`pbx` SET `status` = 'enabled' WHERE `pbx`.`pbx_id` = 22;
if ($query) {
header("Location:routing.php");
}
if (!$query) {
die('Could not query:' . mysql_error());
}
}
?>
<?php
$dbhost='localhost';
$dbuser='root';
$dbpass=123;
$dbname='asterisk';
$db = mysql_connect($dbhost, $dbuser, $dbpass) or die("Couldn't connect to the database.");
if($_GET[ID]>0&& $_GET[del]==1){
$query = mysql_query("DELETE FROM `pbx` WHERE `pbx_id`= '$_GET[ID]'");
if ($query) {
header("Location:routing.php");
}
if (!$query) {
die('Could not query:' . mysql_error());
}
}
?>
sábado, 7 de febrero de 2015
regular expresion
http://www.zytrax.com/tech/web/regex.htm#brackets
http://www.opensourceforu.com/2012/06/beginners-guide-gnu-grep-basics-regular-expressions/
http://www.opensourceforu.com/2012/06/beginners-guide-gnu-grep-basics-regular-expressions/
ereg_replac
PHP Function ereg_replace()
Advertisements
Syntax
string ereg_replace (string pattern, string replacement, string originalstring); |
Definition and Usage
The ereg_replace() function searches for string specified by pattern and replaces pattern with replacement if found. The ereg_replace() function operates under the same premises as ereg(), except that the functionality is extended to finding and replacing pattern instead of simply locating it.Like ereg(), ereg_replace() is case sensitive.
Return Value
- After the replacement has occurred, the modified string will be returned.
- If no matches are found, the string will remain unchanged.
Example
Following is the piece of code, copy and paste this code into a file and verify the result.<?php $copy_date = "Copyright 1999"; $copy_date = ereg_replace("([0-9]+)", "2000", $copy_date); print $copy_date; ?> |
Copyright 2000
remove space and numeric caracter
remover espacios y letras caracter no numericos
<?php
function onlyAlphanumericAndSpaces($text) {
# allow only alphanumeric
return ereg_replace("[^0-9 ]", "", $text );
}
$string=" E#D re18 09 71 4 4389 ";
$string = str_replace(' ', '', $string);
$string = preg_replace('/\s+/', '', $string);
echo onlyAlphanumericAndSpaces($string);
}
?>
remover caracter no alfa numericos
<?php
function onlyAlphanumericAndSpaces($text) {
# allow only alphanumeric
return ereg_replace("[^0-9 ]", "", $text );
}
$string=" E#D re18 09 71 4 4389 ";
$string = str_replace(' ', '', $string);
$string = preg_replace('/\s+/', '', $string);
echo onlyAlphanumericAndSpaces($string);
}
?>
remover caracter no alfa numericos
function onlyAlphanumericAndSpaces($text) { # allow only alphanumeric return ereg_replace("[^A-Za-z0-9 ]", "", $text ); }
remove space and whitespace from a variable
For just spaces, use str_replace:
For all whitespace, use preg_replace:
$string = str_replace(' ', '', $string);
For all whitespace, use preg_replace:
$string = preg_replace('/\s+/', '', $string);
http://stackoverflow.com/questions/2109325/how-to-strip-all-spaces-out-of-a-string-in-php
<?php
function string_to_filename($word) {
$tmp = preg_replace('/^\W+|\W+$/', '', $word); // remove all non-alphanumeric chars at begin & end of string
$tmp = preg_replace('/\s+/', '_', $tmp); // compress internal whitespace and replace with _
return strtolower(preg_replace('/\W-/', '', $tmp)); // remove all non-alphanumeric chars except _ and -}?>
mySQL select IN range
Is it possible to define a range for the IN part of the query, something like this
Instead of
|
||||
add a comment
|
You can't, but you can use BETWEEN
|
Suscribirse a:
Entradas (Atom)