PHPreferencesimplePHP Class | Spend more time innovating.


Back to top
Description



1.0 Basic Function

Clean up text by removing potentially dangerous tags

You can now easily clean any text by removing unwanted tags. Simply give a string or a text and it will output a safe version of it. It is as simple as that. Currently it only accepts <a><b><i><s><u><p><pre><code> but you can easily allow more tags if you like (open General.class.php, find the function "clean" and add your tag.). It also removes completely any script tag (prevents XSS attacks). and automatically adds rel="nofollow" to all links.

Parameter Default Value Required Description
text none Yes Input the text that you want to "clean"
echo $simple->clean("<a href='http://www.google.com'>Link</a> <b>hello</b> <script>alert('This will be removed')</script>");
<a rel="nofollow" href='http://www.google.com'>Link</a> <b>hello</b> 
echo $simple->clean("<a href='http://www.google.com'>Link</a> <b>hello</b> <sCrIpT>alert('Protects against XSS attacks')</ScRiPt>");
<a rel="nofollow" href='http://www.google.com'>Link</a> <b>hello</b> 

Generate Clean URLs

This function is useful if you would like to generate clean a URL. A Clean URL is an effective strategy to attract more visitor to your site from search engines. Instead of having something like this http://mysite.com/index.php?id=1, you could have http://mysite.com/the-title-of-your-post/1. To generate this, simply use the function "slug" and give it a text or string and it will return you the string into a search-engine friendly format.

Parameter Default Value Required Description
text none Yes Input the text that you want to convert
echo $simple->slug("This could be the title of your post (or a video)?");
this-could-be-the-title-of-your-post-or-a-video

Truncate Text

This function will truncate (cut) a text. All it needs is a text and maximum number of characters that you want to show. All the rest will get cut. It will also add "..." at the end. The first parameter is the text while the second is the length you want show.

Parameter Default Value Required Description
text none Yes Input the text that you want to cut.
number (integer) none Yes Number of characters you want to show
suffix ... No What do you want to show at the end...
echo $simple->truncate("Lorem ipsum aliquip ut consequat do ut culpa proident non mollit pariatur in proident deserunt veniam amet ut anim ea dolor dolore consequat.",50);
Lorem ipsum aliquip ut consequat do ut culpa proid...
echo $simple->truncate("Lorem ipsum aliquip ut consequat do ut culpa proident non mollit pariatur in proident deserunt veniam amet ut anim ea dolor dolore consequat.",50,'...To Be Continued');
Lorem ipsum aliquip ut consequat do ut culpa proid...To Be Continued

Validates an email

This function validates a given email address. Simply give the email and it will return a message if it is not valid otherwise it will not return anything. It has only one parameter and it is the email address.

echo $simple->checkemail("admin@admin");
Please enter a valid email.

Username Check

This function validates a given username. Simply give the username and it will return a message if it is not valid otherwise it will not return anything. It has only one parameter and it is the username.

echo $simple->checkuser('myuser name');
Please enter a valid username.

Generate Random Password

This function will generate a random password every time it is called. The default length is 8, characters but you can easily change it by setting a number as the first parameter (see example).

echo $simple->random_password();
4i$BYbf4

Usage with 5 characters

echo $simple->random_password('5');
xKQuL

Encode a Password

This function encodes a password. There are three parameters. The first one is the user's password. The second one is the encoding algorithm (MD5 , SHA1 or SHA256). The Third one is a security key which will be added to the password automatically and then encoded. The default encoding is MD5 and no salt.

Parameter Default Value Required Description
password none Yes The Password you want to encode
encoding MD5 No Encoding algorithm: MD5, SHA1 or SHA256
salt none No A key that will increase security (number or text)
echo $simple->encode_password('mypassword','SHA1','sample salt');
fffbe66da91856c3ab6d00750317eb21eb3b47be

Usage with MD5

echo $simple->encode_password('mypassword','MD5','sample salt');
6729003b18b2e4be04d0b97d1eeb8423

Usage with no Salt

echo $simple->encode_password('mypassword','SHA1');
91dfd9ddb4198affc5c194cd8ce6d338fde470e2

Simple Encoding

echo $simple->encode_password('mypassword');
34819d7beeabb9260a5c854bc85b3e44

Coverts Date & Time into Time ago.

This function converts a date AND time string into XX time ago. Simply give the date AND time string in this format: YYYY-MM-DD HH:II:SS and it will return you the time ago.

echo $simple->timeago("2012-05-08 09:45:00");
1 year ago 

Format Date

This function returns you a formated date. Simply provide it the date AND time in this format: YYYY-MM-DD HH:II:SS and it will return a formatted date. There is also a second parameter: if you only want to show the date, set the second parameter to FALSE. This will remove the formatted time.

Parameter Default Value Required Description
date none Yes Complete date e.g. 2012-10-01 09:45:00
time TRUE No Set it to False to show only Date
echo $simple->formatdate("2012-05-08 09:45:00");
October 1, 2012 at 09:45am

Usage Date Only

echo $simple->formatdate("2012-05-08 09:45:00", FALSE);
October 1, 2012

Format Numbers

This function returns you a formated numbers. Simply provide a number and it will return a decimal. The second parameter is the number of decimal places.

Parameter Default Value Required Description
number none Yes The number you want to format
decimal 0 No Number of decimals to show
echo $simple->formatnumber("2500000",1);
2.5M
echo $simple->formatnumber("250000000",1);
250M
echo $simple->formatnumber("9876543109",2);
9.88B

Format Price

This function returns you a formated price. Simply give the price and it will output the price and format the price using whatever currency you are using.

Parameter Default Value Required Description
price none Yes The price you want to format
currency USD No USD/CAD/EUR/GBP/AUD/NZD/DDK/CNY
cent FALSE No Setting it to TRUE will output the cents into this format 99 99
echo $simple->formatprice("1999.99");
$1999.99
echo $simple->formatprice("1999.99","CAD",TRUE);
C$199999

Converts BBcode into HTML

This function converts the following bbcodes into html.
[b]TEXT[/b]
[i]TEXT[/i]
[u]TEXT[/u]
[size=NUMBER]TEST[/size] (e.g. 12 which would result in 12px)
[color=HEXADECIMAL]TEXT[/color] (e.g. #ffffff)
[url=LINK]TEXT[/url]
[img]LINK[/img]
[video]LINK[/video] (either youtube or vimeo)
You can easily add your own bbcode, simply open the main class, find the function "bbcode" and follow examples.

echo $simple->bbcode("[b]Hello[/b], How are [u]you[/u]? I am [i]BBcode[/i] and this is my [url=http://www.google.com]website[/url]. This is my video [video]http://www.youtube.com/watch?v=V70ruRzEGOI[/video].");
<b>Hello</b>, How are <u>you</u>? I am <i>BBcode</i> and this is my <a href="http://www.google.com">website</a>. This is my video <div class="video"><iframe width="400" height="250" src="http://www.youtube.com/embed/V70ruRzEGOI?theme=dark&iv_load_policy=3&wmode=transparent" frameborder="0" allowfullscreen></iframe></div>.

Actual Output

Hello, How are you? I am BBcode and this is my website. This is my video
.

Get File Extension

This is simple but very useful. It will get you the extension of any file. It can be used to validate a file uploaded by a user for example.

echo $simple->getextension("filename.extension");
.extension
echo $simple->getextension("filename.secondname.2012.pdf");
.pdf

Generate Thumbnail

Now you can easily generate thumbnails with following function. It has two required parameters and two optional. The first parameter is the source of the image along with its complete path. So if the image is in images/file.png you should input "images/file.png". Usually you don't need this because when you upload a file the path is automatically inserted. In this case simply use $_FILES["inputfieldname"]["tmp_name"]. The second parameter is the path you want to store the thumbnail including the name of the thumbnail. If you want to store in images/thumb folder and want to name it "thumb1.jpg" you must use "images/thumb/thumb1.jpg". Note that this parameter is optional. If you don't set, it will generate the thumbnail in the same directory as the orginal file and name "originalfilenamethumbwidth.extension". The third parameter is the width of the thumb. It is a required parameter and it is in pixels e.g. 300 = 300px. And finally the last parameter is the quality. If you don't set this it will assume 100% quality.

This function will output you an array. The first value tells you whether or not there is an error. If there is an error it will be set to TRUE otherwise to FALSE. The second value is the error message. The third one is path which shown if and only if resizing was successful. Final Note: This function only resizes jpeg/jpg, png and gif.

Parameter Default Value Required Description
filename none Yes Original Filename with its path
thumbfilename Same directory as original, filename: originalwidth.ext No The name of the thumbnail with its path
width (integer) none Yes The width (px) of the thumbnail
Quality (integer) 100 No The quality of the thumbnail
echo $simple->thumb('asset/template.jpg','',300);
Array
(
    [error] => 
    [msg] => Image has been successfully resized.
    [thumb] => asset/template300.jpg
)

Usage (Example of error)

echo $simple->thumb('asset/filedoesnotexist.jpg','',300);
Array
(
    [error] => 1
    [msg] => This file (asset/filedoesnotexist.jpg) doesn't exist.
)

Example of integration

$thumb=$simple->thumb('asset/template.jpg','',300);
	if(!$thumb["error"]){
		echo "Thumb has been generate and no error has occurred. Here is the link to the thumbnail http://www.mysite.com/{$thumb["thumb"]}";
	}
	Thumb has been generate and no error has occurred. Here is the link to the thumbnail http://www.mysite.com/asset/template300.jpg

Shorten URL with tinyurl or goo.gl or is.gd

If you want to generate a shorten url simply use the function shorten_url(). It has two parameters. The first one is the URL which is required. The second one is the provider (either tinyurl or goo.gl for now), the default one is tinyurl.

Parameter Default Value Required Description
URL none Yes The URL to shorten
provider tinyurl No Either "google" for (goo.gl) or "tinyurl" for tinyurl.com or "isgd" for is.gd
echo $simple->shorten_url('http://gempixel.com/simplephp/');
http://tinyurl.com/b7cjrsb

Usage with goo.gl

echo $simple->shorten_url('http://gempixel.com/simplephp/','google');
http://goo.gl/2OYBJ

Usage with is.gd

echo $simple->shorten_url('http://gempixel.com/simplephp/','isgd');
http://is.gd/XrB8VB

Unshorten URL

This function simply unshortens the short urls. It currently supports goo.gl and is.gd. Tiny URL doesn't provide a public api for unshortening.

Parameter Default Value Required Description
short URL none Yes The URL to unshorten
echo $simple->unshorten_url('http://goo.gl/2OYBJ');
http://gempixel.com/simplephp/

Image Upload

You can now quickly upload and resize an image using the upload_image() function. I recommend you only use this to upload image files because uploading other types of file requires a much advanced function and a tighter security.

This function has 5 parameters. The first one is the folder to upload your file to. If this is not set it will automatically create an "upload" folder and upload to it. The second parameter is the file (you can use $_FILES). The third parameter is used to create a thumbnail (if you want to generate one, set it to TRUE). The fourth parameter is the thumbnail width (default is 300). The fourth parameter is the thumbnail folder (if not set it will create a "thumb" folder in "upload"). The function will output an array. If everything is successful, it will output success messages and the path to the file and the thumbnail.

Parameter Default Value Required Description
folder upload/ Yes The folder you want to upload. If not set, it will create an "upload".
filename none Yes The file you want to upload.
thumbnail False No If you want to generate a thumbnail set it to "TRUE".
thumbnail width 300 No The width of your thumbnail.
thumbnail folder upload/thumb/ No The thumbnail folder. If not set it will create a "thumb" folder in "upload".

Simple Upload

print_r($simple->upload_image('myfolder/', 'template.jpg', TRUE));
Array
(
    [file] => File has been successfully uploaded.
    [filepath] => myfolder/template.jpg
    [thumb] => Image has been successfully resized.
    [thumb_path] => myfolder/thumb/template.jpg
)

Sample Usage

<?php
	if(isset($_POST['file'])){ //Check if file var is set
		require("General.class.php"); // include the class
		$simple=new General(); //instantiate the class
		$upload=$simple->upload_image('myfolder/', $_FILES['upload'], TRUE); //basic usage
		var_dump($upload); // outputs all messages (not required)
	}
?>

<form action="" method="post" enctype="multipart/form-data">
	Note: name="ABCD" of file tag below has be consistent with $_FILES["ABCD"] above
<input type="file" name="upload"> 
<input type="submit" name="file">
</form>	

Social Count

You can now use this function to get the number of shares or tweets of the current page. It has two parameters: the first one is the site (facebook or twitter) and the second one is the URL. The function automatically grabs the URL so no need to set it.
Parameter Default Value Required Description
Site twitter No "twitter" or "facebook"
url none No If you want set the url manually, you can use the second parameter, otherwise it will grab it automatically.
echo $simple->social();
0

Custom URL

echo $simple->social('twitter','http://gempixel.com')." Tweets";
echo $simple->social('facebook','http://gempixel.com')." Likes";
51 Tweets
60 Likes

2.0 Notification Messages

To show notification messages, you would need to do two steps. First: Set the message using the setmsg() function and then show it using showmsg(). To set the message simply call setmsg() and give the message content as the first parameter (can be html) and the message type as the second parameter. The second paramater is mainly used for styling purpose (error, sucess, warning, notice or it can your own style ex. download). The showmsg() function doesn't require any parameter. To show the message simply echo showmsg(). 4 styles are already included. Open style.css and find copy everything that starts with .message

NOTE: Message are stored in a session variable name msg. Once set, it will stay there until you call showmsg() or the user closes the browser or you overwrite the message with another one.


Parameter Default Value Required Description
message none Yes The message to show (can be html)
type none No Mainly used for styling e.g. "error"

Example HTML Output(Notice the class is message AND error)

<div class='message error'>This is an error message.</div>	

Usage (4 styles)

$simple->setmsg('This is an error message.', 'error');
	echo $simple->showmsg();
$simple->setmsg('This is a success message.', 'success');
	echo $simple->showmsg();	
$simple->setmsg('This is just a warning.', 'warning');
	echo $simple->showmsg();	
$simple->setmsg('And this is  a notice', 'notice');
	echo $simple->showmsg();	

HTML Output

This is an error message.
This is a success message.
This is just a warning.
And this is a notice

3.0 Grabs Video from Links

One great feature of this class is its ability to grab the video for Youtube and/or Vimeo. There are three functions for this feature, one that one grabs Youtube video, one that grabs video from Vimeo and the other one simply replaces all Youtube and Vimeo links with a text.

Grab Video from Youtube Links

This one replaces all Youtube link into videos. It has four parameters (One which is required). The first one is the URL to the video, the second one is the width of player (default value is 400), the third is height of the player (default value is 250) and the last one is the theme (either light or dark, default is dark).

Parameter Default Value Required Description
url none Yes The url to the video
width (integer) 400 No The width of the player in pixels
height (integer) 250 No The height of the player in pixels
theme dark No Color of the player. Either "dark" or "light"

Usage (Default Settings)

echo $simple->youtube("http://www.youtube.com/watch?v=V70ruRzEGOI");

Usage with width=250, height=125 and theme=light

echo $simple->youtube("http://www.youtube.com/watch?v=V70ruRzEGOI",'250','125','light');

Grab Video from Vimeo Links

Exactly the same thing as above except Vimeo doesn't have the theme parameter only URL, width and height.

Parameter Default Value Required Description
url none Yes The url to the video
width (integer) 400 No The width of the player in pixels
height (integer) 250 No The height of the player in pixels
echo $simple->vimeo("http://vimeo.com/43183234");

Automatically Transform Youtube & Vimeo Link into Video

Now this function is really cool. What it does is it basically analyzes the given text, extracts all links and replace Youtube and Vimeo links with videos. It will remove all links and put all videos at the end of the text (see example). You can also set the width and the height of the video (not required) by setting the numbers as the second and third variables, respectively. It will not remove any other links expect Youtube and Vimeo.

Parameter Default Value Required Description
text none Yes The text containing the URL to youtube or Vimeo
width (integer) 400 No The width of the player in pixels
height (integer) 250 No The height of the player in pixels
echo $simple->video("This function can automatically transform this link http://vimeo.com/43183234 to video and it does the same for youtube http://www.youtube.com/watch?v=V70ruRzEGOI.");
This function can automatically transform this link to video and it does the same for youtube .

Example 2 (note that other links are not removed)

echo $simple->video("This link http://www.google.com will not be removed.");
This link http://www.google.com will not be removed.

4.0 Generates Menus

Page Navigation

This is another very useful function. It generates a pagination for any page. It requires three parameters: the first one is the total number of pages, the second one is the current page and the third one is the format of the URL. For the format put %d (which tells that it has to be an integer) where you want the page numbers to appear in your FULL URL. Example http://www.mysite.com/?page=%d will result in http://www.mysite.com/?page=1 , http://www.mysite.com/?page=2 , http://www.mysite.com/?page=3 , etc... As a bonus I have included a beautiful CSS3 pagination template. If you want to edit the template simply change the styles of the .pagination class. To copy the included style, open style.css in the asset/ folder and find "div.pagination" and copy all styles that have this before.

Parameter Default Value Required Description
totalpages (integer) none Yes The total number of pages you have
current (integer) none Yes The current page.
url none Yes The url to display. Put "%d" where you want the page numbers to appear
echo $simple->pagination(8,2,'http://gempixel.com/simplephp?page=%d');
<div class="pagination"><a href="http://gempixel.com/simplephp?page=1">Previous</a> <a href="http://gempixel.com/simplephp?page=1"">1</a> <span class="current">2</span> <a href="http://gempixel.com/simplephp?page=3"">3</a> <a href="http://gempixel.com/simplephp?page=4"">4</a> <a href="http://gempixel.com/simplephp?page=5"">5</a> ... <a href="http://gempixel.com/simplephp?page=8">8</a> <a href="http://gempixel.com/simplephp?page=3">Next</a> </div>

HTML UI

5.0 Generates Commenting Systems

Social plugin are now starting to replace native systems. These functions are designed to output the Facebook Plugin and Disqus by only using one line of code.

Facebook Social Plugin

To show the Facebook social plugin simply call facebook_comment() and that is it. This function has four parameters (not required). The first one is the url of your page. If you don't set this, the function will automatically get it but it might also included queries such as ?q=Something or ?page=somethingelse. If you want to be safe, manually put the first parameter for example facebook_comment('http://www.google.com'). The second parameter is the number of post you want to show, default is 4. The third one is the width of the plugin in pixels. To make it responsive (i.e. it automatically adapts itself to any browser including mobile) simply put width as '0'. And the last parameter is the theme (either Dark or Light, Default is Light)

Parameter Default Value Required Description
url Current Page URL No The url of the current page. It automatically gets it.
posts (integer) 4 No The number of comments to show
width (integer) 700 No The width of the layout in "px". Put "0" to make is responsive.
theme light No Either "light" or "dark"
echo $simple->facebook_comment();

Usage (Responsive with 10 posts)

echo $simple->facebook_comment('','10','0');

Disqus Commenting System

This one outputs the disqus commenting system and it requires only one parameter, your username. You must first register here and simply use the command below to show it. If you don't put your username it will not show up. Disqus is already reponsive by default so need to adjust things. That's it.

echo $simple->disqus_comment('kbrmedia');