Monthly Archives: September 2015

WordPress Sharing to Promote your Blog

I can not emphasize the importance of sharing enough if you wish to gain a respectable amount of traffic on your WordPress blog or website! The Jetpack plugin has just about all the share features you could want outside of some special interests of course. I personally use Jetpack to connect this WordPress site to my Facebook, Twitter, Google+, Pinterest, LinkedIn, and eMail. Tumblr, StumbleUpon, Reddit and Pocket are also readily available. Use as many of these services as you can(at least 4 or 5) and you'll notice traffic increased to your site soon! You also have to publish regularly as that's what triggers sharing, but that's why you have a blog right? Get to work!

Can't Find Sharing Settings in WordPress?

Sharing settings are located in wp-admin under Settings/Sharing. If the Sharing link doesn't appear under the main settings link in wp-admin, that most likely means that you do not have JetPack installed. Install JetPack and your sharing link should appear instantly. Once you click on settings, the sharing link will appear as a sub-link under settings.

Signing up for Social Media Sites

JetPack and WordPress sharing do not work without social media, so you will need to sign up for as many social media sites included in the JetPack/WordPress sharing options as possible! You can sign up to all of them very easily simply by installing JetPack, navigating to wp-admin/settings/sharing and clicking on the various service links to establish connections and sign up for any that you have not already signed up for.

Summary

I only wrote this simple advice on my blog because I think it is something that a lot of people over look. It's a shame to not participate in such a simple method to increase traffic to your blog.  Most people think they will have to post to each of the social media sites individually and assume it will be too much work, but that's not quite true because if you use the JetPack plugin with WordPress, it will post to most of the popular social medial sharing sites for you every time you write a new post or page on your WordPress site! This makes life nice and easy, so try it today!

Check String for Any of a Group of Characters with PHP

Today I found a much easier way to check a string for a designated group of characters. For example, in this case I had to check to see if a string had any numbers or dashes in it and discard it if it did. I discovered PHP has a less known function for this purpose called strpbrk. Not so sure what strpbrk stands for, but it works great. Here is how I used it:

<?php

$string = "abc9.com";

if(strpbrk($string, '0123456789-')!==false) {
echo "No Good(string contains numbers or dash)...skipped<br />";
}//end if string has numbers or dash

?>

There you have it, nice and easy and fast too!

Quick Way to Check if String Exists in a File with PHP

Today I had a challenge where I needed to search a rather large .txt file for a string. Reading the entire .txt file into memory would not work because it would time out due to other resource intensive operations of the program I was creating. That's why I had to find a fast way to check a file to see if it contained a line of text. In my case, I was searching a .txt file with over 100k domains listed in it for a single domain name, but this would work with any type of file containing text and any type of search string. Here was the code I found that worked the best in PHP:

  <?php
$myFile = "domains/domain_list.txt";

$searcString = "what your looking for";
            if(file_exists($myFile)){//if myFile exists check it for searchString:
                if(exec('grep '.escapeshellarg($searcString).' '.$myFile)) {
                echo "***ALERT! $searchString already exists in $myFile!!!<br>";
                }//end if found searchString in myFile
            }//end if file exists

?>

And that did the trick! You may or may not want the to use the file_exists check first, but I put it there for good measure because in my case, sometimes the file hadn't been created yet, so why check it? The simple working version without the file check feature would be simply:

<?php
 $myFile = "domains/domain_list.txt";

$searchString = "Text you want to find";
                if(exec('grep '.escapeshellarg($searchString).' '.$myFile)) {
                echo "***ALERT! $searchStringalready exists in $myFile!!!<br>";
                }

?>

...so just replace myFile and searchString variable contents with your own and you've got what you need to find any text string in any file using PHP! Fast!

PHP Check if File Exists

It's often necessary to verify if a file exists or not in PHP. Here is how:

<?php
$fn = "dir_path/file_name.txt";
    if(file_exists($fn)){
    echo "$fn exists!";
    }else{
    echo "$fn does not exist.";
    }
?>

Simply replace dir_path and file_name.txt with the path to the file you wish to check for and you're done!

Also note that the same method can be used to check if a directory exists as well, simply exclude the file name portion of the $fn variable in the above example