PHP String Contains

PHP String Contains is a simple function I created. Quiet similar to strpos or strstr, but in my case, I have used this in most of my projects since its function name is more readable and understandable. I find it to be cleaner, also, as it is returning boolean instead of int.

<?php
if (!function_exists('str_contains')){
/**
 * Checks if a specified substring occurs within this string.
 * @param string $haystack : The string to test.
 * @param string $needle : The string to seek.
 * @param string $offset : (Optional) Starts searching from specified index.
 */
function str_contains($haystack, $needle, $offset = 0){
	return (bool) strpos($haystack, $needle, $offset);
}
/**
 * Checks if a specified substring occurs within this string. (case insensitive)
 * @param string $haystack : The string to test.
 * @param string $needle : The string to seek.
 * @param string $offset : (Optional) Starts searching from specified index.
 */
function str_icontains($haystack, $needle, $offset = 0){
	return (bool) stripos($haystack, $needle, $offset);
}
}

It works by checking if a given needle is found in the haystack using strpos function. You can also define offset so it starts searching from the specified index.

Equivalent functions are provided for both case-sensitive and case-insensitive.

Examples

<?php
str_contains ( 'The quick little brown fox jumps over the lazy dog.', 'The' );
// Result: true

str_contains ( 'The quick little brown fox jumps over the lazy dog.', 'THE' );
// Result: false

str_icontains ( 'The quick little brown fox jumps over the lazy dog.', 'THE' );
// Result: true

You can see more of my PHP gists here.

PHP Email Forwarder

An Automatic IMAP/POP3 Email Forwarder

About this project

PHP Email Forwarder automatically gets all unread message from a specified IMAP/POP3 email account. Then forwards using PHP Mail() it to another email address or list of email addresses. You may use this for your company email, in which, messages received can be forwarded to multiple employees.

Setting up

  • Copy the files to a specific directory.
  • Create a copy of settings.sample.php and rename it as settings.php.
  • Fill in the fields required in the file.
  • An instance of the settings will look like this:
<?php
// ... (removed code)
 'in' => [
     'host' => 'imap.yourdomain.com',
     'port' => 995,
     'type' => 'imap', // can be: pop3 or imap, note: seen/unseen does not work on pop3
     'security' => 'ssl' // can be: ssl, tls, notls
 ],

 // username and password for incoming mail box
 'user' => 'user@example.com',
 'password' => 'password',

 // where to send
 'to' => ['new_user@example', 'new_user_2@example.com'],

 'delete_copy' => false // when set to true message will be delete after forwarded
  • You can create more instances to watch another email.
  • Create a cron task to run /usr/bin/php task.php every 5 minutes or so.

Image Taken at Suzhou, Jiangsu, China, edited with Adobe Photoshop CC 2017.

Better Error Handler for WordPress Debugging

As a theme or plugin developer, WordPress debugging may be a pain as some errors may not be specific, especially if you have a giant class->function call stack. With this simple WordPress Error Handler script, you can view the call stack of the errors you get.

Overview

  1. Create wp-error-handler.php file in your wp-content/mu-plugins directory.
  2. Copy the contents as provided in the source code below.
  3. Setup WP_DEBUGWP_DEBUG_DISPLAY, or WP_DEBUG_LOG in wp_config.php to true.

Setting it Up

Below is the code provided you need to set up. In your wp-content/mu-plugins directory create a file wp-error-handler.php. WordPress will automatically load the file as it is a Must Use Plugin. You may read more about must use plugins here.

You have to define the constant WP_DEBUG as true to enable the error handler. To display errors on your web browser, set WP_DEBUG_DISPLAY as true.

You also have the choice to write the errors to the WordPress’s debug log file (/wp-content/debug.log) by enabling WP_DEBUG_LOG constant.

In an event of an error, a message similar to the screenshot below will be displayed.

Example Screenshot

Image Taken at Suzhou, Jiangsu, China, edited with Prisma AI.

PHP Combine Path

PHP Combine Path is a simple function I created that is roughly equivalent to Microsoft .NET’s Path.Combine method. This function combines two strings into a path. However, this only handles when to place path separators in between the two strings given, whether given string is a valid path or not.

It works by trimming off separator characters on the left side of $path1 and the right side of $path2, which is then concatenated as $path1, the directory separator character, and $path2. The function also works with URLs.

<?php
if (!function_exists('str_combinepath')){
/**
 * Combines two strings into a path.
 * @param string $path1 : The first path to combine.
 * @param string $path2 : The second path to combine.
 * @param string $separator : (Optional) The directory separator. Default: OS specified.
 */
function str_combinepath($path1, $path2, $separator = DIRECTORY_SEPARATOR){
	$path1 = rtrim($path1, $separator);
	$path2 = ltrim($path2, $separator);
	return $path1 . $separator . $path2;
}
}

Examples

<?php
str_combinepath ('/home', 'usr/');
// Result: '/home/usr/'

str_combinepath ('/home/', 'usr/');
// Result: '/home/usr/'

str_combinepath ('c:\temp', 'subdir\file.txt', '\\');
// Result: 'C:\temp\subdir\file.txt'

// Also works for URL's
str_combinepath ('https://www.earlpeter.com', 'wp-admin', '/');
// Result: https://www.earlpeter.com/wp-admin

You can see more of my PHP gists here.


Image Taken at Suzhou, Jiangsu, China, edited with Prisma AI.