function startsWith($needle, $haystack){
return preg_match('/^'.preg_quote($needle)."/", $haystack);
}
function endsWith($needle, $haystack){
return preg_match("/".preg_quote($needle) .'$/', $haystack);
}
Showing posts with label PHP. Show all posts
Showing posts with label PHP. Show all posts
PHP: startsWith endsWith functions
PHP: exec command not working; sh: command not found
When using PHP exec command, if you find sh: Command not found , error in your web server logs, you have to export the full path of that command.
Assuming, that command in /opt/local/bin, change the command to
To debug PHP exec command, Read this
For example:
For the PHP code:
There will be no output. In your web server error log, your will have :
Go to your terminal, find where your command is located, In this example:
So, modify you PHP code as following:
This will output, on your web page
Assuming, that command in /opt/local/bin, change the command to
$cmd ='export PATH=$PATH:/opt/local/bin; myCommand 2>&1';
echo exec($cmd,$out);
To debug PHP exec command, Read this
For example:
For the PHP code:
$cmd = "gmake";
echo exec($cmd,$out);
There will be no output. In your web server error log, your will have :
sh: gmake: command not found
To fix this, we need to export the PATH of the gmake.Go to your terminal, find where your command is located, In this example:
$ which gmake
/opt/local/bin/gmake
So, modify you PHP code as following:
$cmd ='export PATH=$PATH:/opt/local/bin; gmake 2>&1';
echo exec($cmd,$out) ;
This will output, on your web page
gmake: *** No targets specified and no makefile found. Stop.
PHP: exec command not working; debugging exec command
While using PHP exec command, if it is not working , Follow these to debug
1. Check your web server error_log.
Usually in MAC, for APACHE web server, it is located at /var/log/apache2/
1.1 Case1: the web server does not have permission to read/execute a command/file.
Solution to Case1: Simple, change file permissions.
1.2 Case2: The exec command will throw sh: Command Not found
Solution to Case2: Read this
2. Append 2>&1 to your command to get std errors as well. Read this for more info
1. Check your web server error_log.
Usually in MAC, for APACHE web server, it is located at /var/log/apache2/
1.1 Case1: the web server does not have permission to read/execute a command/file.
Solution to Case1: Simple, change file permissions.
1.2 Case2: The exec command will throw sh: Command Not found
Solution to Case2: Read this
2. Append 2>&1 to your command to get std errors as well. Read this for more info
PHP: Today, Yesterday, Tomorrow dates
In PHP, we can generate Today, Yesterday, Tomorrow Dates the following way:
$today = date('Y-m-d');
$tomorrow = date('Y-m-d', strtotime('tomorrow'));
$yesterday = date('Y-m-d', strtotime('yesterday'));
PHP: exec command to get std errors
By appending 2>&1 at the end of the command will redirect errors from stderr to stdout.
Example:
If you run a simple command like cc at the command prompt, you would get the output as following
Example:
If you run a simple command like cc at the command prompt, you would get the output as following
$ cc
i686-apple-darwin10-gcc-4.2.1: no input files
$
If u run the same command using PHP exec command $cmd = "cc";
exec($cmd,$out);
print_r($out);
The output would be and empty array: Array ( )
So, In order to get the error string as well, we need to append 2>&1 $cmd = "cc 2>&1";
exec($cmd,$out);
print_r($out);
The output will print: Array ( [0] => i686-apple-darwin10-gcc-4.2.1: no input files )
PHP: Comparing dates/date strings in PHP
Comparing Date strings in PHP is easy using strtotime function.
The strtotime function returns # of seconds since Jan 1,1970 UTC.
Simple example:
$strtDate = strtotime("2009-10-3");
$endDate = strtotime("2010/11/9");
if($strtDate > $endDate){
// do something
}
if($strtDate < $endDate){
/do something
}
The strtotime function returns # of seconds since Jan 1,1970 UTC.
Simple example:
$strtDate = strtotime("2009-10-3");
$endDate = strtotime("2010/11/9");
if($strtDate > $endDate){
// do something
}
if($strtDate < $endDate){
/do something
}
Fixing Warning (2): Cannot modify header information - headers already sent by (output started at...
How to fix the following error in PHP
Warning (2): Cannot modify header information - headers already sent by (output started at ...
This is also referred to as "whitespace problem".
The error message typically looks something like
Warning (2): Cannot modify header information - headers already sent by (output started at /Library/WebServer/Documents/test-cake/app/models/post.php:1) [CORE/cake/libs/controller/controller.php, line 644]
Steps to resolve the issue:
1) Find the header() statement that is causing the problem. The error must be at or before this line.
2) Identify statements that could send output to the user before this header statement. Example 'echo ' statements. You cannot have these 'echo' statements before header() statement.
3) Make sure there is no white space before the php start and end tags.
Also at the beginning of the file a blank line before the <?php start tag may look innocent, when processed by PHP, it will turn into an echo statement printing out a blank line. This is a common culprit.
This is also referred to as "whitespace problem".
The error message typically looks something like
Warning (2): Cannot modify header information - headers already sent by (output started at /Library/WebServer/Documents/test-cake/app/models/post.php:1) [CORE/cake/libs/controller/controller.php, line 644]
Steps to resolve the issue:
1) Find the header() statement that is causing the problem. The error must be at or before this line.
2) Identify statements that could send output to the user before this header statement. Example 'echo ' statements. You cannot have these 'echo' statements before header() statement.
3) Make sure there is no white space before the php start and end tags.
Also at the beginning of the file a blank line before the <?php start tag may look innocent, when processed by PHP, it will turn into an echo statement printing out a blank line. This is a common culprit.
Mac OS X Fixing Warning: mysql_connect(): Can't connect to local MySQL server through socket '/var/mysql/mysql.sock'
How to fix Warning: mysql_connect(): Can't connect to local MySQL server through socket '/var/mysql/mysql.sock'
Apparently, Mac OS X doesn't allow for PHP MySQL connection out of the box. You get this:
Warning: mysql_connect(): Can't connect to local MySQL server through socket '/var/mysql/mysql.sock'
OR In CakePHP
Warning: Could not connect to Database.
I found the most clear fix here:
/etc/php.ini(.default) looks for mysql.sock in the wrong place... two options are to make a symbolic link from the right place to the socket...
sudo mkdir /var/mysql
sudo ln -s /private/tmp/mysql.sock /var/mysql/mysql.sock
Or you can update your php.ini (.default) by finding "mysql.default_socket" and setting it to equal /private/tmp/mysql.sock and then restart apache web server
Apparently, Mac OS X doesn't allow for PHP MySQL connection out of the box. You get this:
Warning: mysql_connect(): Can't connect to local MySQL server through socket '/var/mysql/mysql.sock'
OR In CakePHP
Warning: Could not connect to Database.
I found the most clear fix here:
/etc/php.ini(.default) looks for mysql.sock in the wrong place... two options are to make a symbolic link from the right place to the socket...
sudo mkdir /var/mysql
sudo ln -s /private/tmp/mysql.sock /var/mysql/mysql.sock
Or you can update your php.ini (.default) by finding "mysql.default_socket" and setting it to equal /private/tmp/mysql.sock and then restart apache web server
Get names of month, weekday of the year for any date value in PHP
To get the names of month, weekday, of the year for any date value in PHP, use the function getDate();
Example:
$datePieces = getDate(strtotime($mydate));
echo "Month name: ".$datePieces['month'];
echo "Weekday name: ".$datePieces['weekday'];
Make sure to use quotes for the key value like $datePieces['month'] instead $datePieces[month], since it would throw : Notice: Use of undefined constant ....
The associative array $datePieces contains all of the following:
Array ( [seconds] => 40 [minutes] => 58 [hours] => 21 [mday] => 17 [wday] => 2 [mon] => 6 [year] => 2003 [yday] => 167 [weekday] => Tuesday [month] => June [0] => 1055901520 )
Subscribe to:
Posts (Atom)
Python contextlib for Timing Python code
If you've ever found yourself needing to measure the execution time of specific portions of your Python code, the `contextlib` module o...
-
If you get the following error while installing FFMPEG or other packages on CentOS 7. Requires: libva.so.1(VA_API_0.33.0)(64bit) Follow...
-
Common error: fallocate: fallocate failed: Text file busy in ubuntu OS Check the allocation: free -m or sudo swapon --show Make sure ...
-
How to fix Warning: mysql_connect(): Can't connect to local MySQL server through socket '/var/mysql/mysql.sock' Apparently, Ma...