Category: PHP

PHP Mail (Send Email)

If you have a contact us page or something that sends an email with the use of php, it will use the “mail()” function of PHP. Here is the function:

[code lang=”php”]mail(‘Send To’,’Email Title’,’Email Message’,’Headers’,’Parameters’);[/code]

Send To = Who the email is being sent to.
Email Title = Title of the email, appears in inbox.
Email Message = The body text of the message sent.
Headers = Specifies additional headers, like From, Cc, and Bcc.
Parameters = Specifies additional parameters.

Here is an example:

[code lang=”php”]

[/code]

You have to be careful with the mail function and using headers/parameters. These are often ways of hackers sending faulty information to the script which can allow for header infections, etc. Always be sure to clean your variables you are sending to the function.

Here is a good way to check an email with preg_match:

[code lang=”php”]
function check_email($str){
if(preg_match(“/^[a-z0-9&\’\.\-_\+]+@[a-z0-9\-]+\.([a-z0-9\-]+\.)*+[a-z]{2}/is”, $str)) {
$email = trim($str);
return $email;
}
}
[/code]

Enjoy!

Filed under: PHP, Web ProgrammingTagged with: ,

PHP preg_replace

Preg_replace in PHP uses regular expressions to replace matches in the subject with the given replacement. The syntax of preg_replace is as follows:

[code lang=”php”]
preg_replace(pattern,replacement,subject [,limit]);
[/code]

A good example of using preg_replace is with simple bbcode and smilies. However with more complex bbcode there are other ways that would proove to be much easier and more useful. Let’s say you want to replace smiley code with the image of your smilies.  Here is how you would do it with preg_replace:

[code lang=”php”]
$str = $_POST[“textbox”];

$smiliesFind = array(
‘/:\)/’,
‘/:P/’,
);

$smiliesReplace = array(
‘,
‘,
);

print preg_replace($bbcodeFind,$bbcodeReplace,$str);[/code]

This will take all of those matches in the input $_POST[‘textbox’] and replace it with the HTML code of the smiley’s image. Now, why does the find code use slashes like this?

When using regular expressions you need a syntax. The slashes provide that regular expression and the backslashes are needed to escape the parts that could be mistaken as something else in the preg_replace.  This goes for anything you want to escape when executing php code.

We can use preg_replace for some bbcode as well:

[code lang=”php”]
$bbcodeFind = array(
‘/\[color\=(.*?)\](.*?)\[\/color\]/is’,
‘/\[b\](.*?)\[\/b\]/is’,
‘/\[i\](.*?)\[\/i\]/is’,
);

$bbcodeReplace = array(
$2‘,
$1‘,
$1‘,
);

print preg_replace($bbcodeFind,$bbcodeReplace,$str);
[/code]

You can see the use of “(.*?)” in the find array. This will apply the code to any text found inside the tags we supply. This is how it would appear:

Green text!
Bold text!
Italic text!

Filed under: PHP, Web ProgrammingTagged with: ,

PHP Classes and Functions

A great way to organize your tons of functions you will most likely have on your website is by using classes. Classes can be used to hold many functions that you group together – usually by their purpose on the website.

For example, you would probably have functions for executing sql queries in a class of something like “sql”.

Here is how a class is shown in PHP:

[code lang=”php”]

[/code]

A class is created with “class” then space, the name of the class, and brackets (opening and closing).

Let’s say you created this class and you want to execute a function out of this class in your php page. To start a class on your page you have to give the class a variable, like so:

[code lang=”php”]
$sql = new sql();

// Execute function.
$sql->doSql(“SELECT * FROM tbl WHERE id = ‘5’”);
[/code]

This will set $sql as the variable for class “sql”. Executing the functions in a class are just the variable plus the arrow and the function.Continue reading

Filed under: PHP, Tutorials, Web ProgrammingTagged with: , ,