Thursday, April 23, 2009

Get file extension in PHP

Here are some simple ways to retrieve extension of a file using PHP. You need this when you try to validate file uploads, for example.

So here is one simple way:

$extension = end(explode(".", $filename ));

Let's say we have: $filename = 'text.jpg' then the result will be jpg.

Another way of getting file extension is using pathinfo() function:
$fileinfo = pathinfo($filename);
$extension = $fileinfo['extension'];

And the third way:
$extension = substr(strrchr($filename,'.'),1);

I use the first method, works just fine. You can use what method you want to determine a file extension.

Thursday, April 9, 2009

Jquery ajax post form

How to post some data using jquery and ajax, and get the response in a div.

First you'll need jquery library.

Then you'll need a html form:


<script type="text/javascript" src="jquery.js></script>
<script type="text/javascript">

$(document).ready(function() {

$("#btn").click(function(){

$.post("ajax.php",{
myfield: $("#myfield").val(),
action: "check"
}, function(msg) {
$("#msg").empty();
$("#msg").html(msg);
});

});

});

</script>

<form action="" method="post">
<input type="text" name="myfield" id="myfield" />
<input type="button" name="btn" id="btn" value="Submit" />
</form>

<div id="msg"></div>

This file it's called index.html


And then the file ajax.php, will take the parameters sent through ajax, and echo the response:

$myfield = $_POST['myfield'];
echo $myfield;

This line returns the value in #msg div.

So there you go, ajax with jquery is pretty simple, you can see here how to send parameters through ajax, and how to get the response into a div. This code can be used to validate user input, emails, number, etc. Instead of $myfield you can echo an error message, or a success message, your choice.

Looking for a PHP or MySQL article?

If you are looking for an article that is not on this site, or looking for a solution regarding PHP or MySQL problem, please comment this post and I'll do my best to give you solutions to your problems as soon as possible.

I am writing these PHP articles and tutorials to help you become a better web developer. All my articles are short and with code examples.

So if you have a "How to...?" question and you need my help (for free :) ) please post it in comments area.

Also if you need a professional web developer, I am available for freelance, providing quality code, security, database optimization, fast websites

Tuesday, April 7, 2009

PHP strip everything but numbers

Stripping non digits in PHP is easy and smooth.

Here is the code:

$digits = eregi_replace('[^[:digit:]]', '', $string );

So having a $string = 'ab345dc-.';

The result would be: $digits = 345;

Using eregi_replace all the non digits charactes will be removed. Filter out everything but numbers using this code.

PHP format days, month in your language

How do you display day and month from a date in your language (other than English)

Eg: 14/03/2009 in English would be: Saturday 14 March 2009. So how do you get "Saturday" and "March" in your language. Easy. Using setlocale function.

Here is a simple example, displaying day and month in Romanian language:

setlocale(LC_ALL, 'ro-RO');
echo iconv('ISO-8859-1', 'UTF-8',strftime("%A, %d %B %Y",strtotime('2009/03/14')));

so strftime is using setlocale to display date and time. inconv function is converting all the weird characters from to UTF-8 so they are displayed properly.

So now you can easily display dates (days and months) in your native language if is other than English, just setlocale to your language and display through strftime.

Wednesday, April 1, 2009

How to parse a PHP file and get contents

file_get_contents function only gets a file contents, but it does not parse it. So if you are interested in parsing a file, and the get its contents, here is the way:

ob_start();
include($filename);
$return_str = ob_get_contents();
ob_end_clean();

So using output buffering, we will include the file, and then get the output result in a variable.