3w PHP Your Source for PHP Source Code

16Jun/110

A simple class for mailing using PHPMailer SMTP Option.

This sample shows how you use the site_mail class within a function.
Notice that chaining is supported here, and makes things much faster in case you want to use the class to mail the webmaster errors from your script.
in this sample we are getting an email sent to us from the site's contact form.

<?
function mail_contact($data)
{
 
	$mail = new site_mail();
	$mail->from($data->email,$data->name)->subject($data->subject);
	$message = 'Sent from...'."\n".'Host: '.gethostbyaddr($_SERVER['REMOTE_ADDR'])."\n";
	$message .= 'IP: '.$_SERVER['REMOTE_ADDR']."\n";
	$message .= 'Name: '.$data->name."\n";
	$message .= 'Email: '.$data->email."\n";
	if($data->link)
	{
		$message .= 'Link: '.$data->link."\n";
	}
	$message .= 'Message: '."\n".$data->message."\n";
 
	$mail->message($message)->html()->logo(LOGO_FILE);
	foreach ($conf->site->admin as $admin)
	{
		$mail->to($admin->email, $admin->name);
	}
	// If you need to debug and make sure your settings are good, uncomment the next line.
	//$mail->debug();
	$sent = $mail->send();
	return $sent;
}
?>

Download: Site Mail class for PHPMailer (1383)
Please comment and make any suggestions you wish.
Thank you.
Manny E.
3w.

Filed under: PHP Classes No Comments
11Nov/101

Function to check if a string exist in another string

Option 1
We call for the position of the string, if it dose not exist, we get FALSE, which is what we check for in option 1.

<?php
function match_str_part($main_str , $needle)
{
	if (empty($main_str) || empty($needle))
	{
		return false;
	}
 
	if (strpos($main_str , $needle) !== FALSE)
	{
		return true;
	}
	return false;
}
?>

Option 2
Simply checking if the string exist as part of another.

<?php
function match_str_part($main_str , $needle)
{
	if (empty($main_str) || empty($needle))
	{
		return false;
	}
 
	if (strstr($main_str , $needle))
	{
		return true;
	}
	return false;
}
?>
Filed under: PHP Functions 1 Comment
11Nov/102

Function to get UNIX time

This will return a UNIX style time stamp...

<?php
function get_unix_time()
{
	return time();
}
?>
Filed under: PHP Functions 2 Comments
14Oct/102

Checking if string exist in array values

In  this sample, we create array with names for a group.
Then we check to see if a specific name is in the group...

Here is the sample.

<?php
// Array of names in group
$array = array('Sam' , 'Louis' , 'Samentha' , 'Joe');
 
if (in_array('Joe',$array))
{
	echo 'Joe is in group...';
}
?>

Thank you and please, do not hesitate to comment.

Filed under: General, PHP Basics 2 Comments
12Oct/101

Ask a programmer php question

Here you can use the comments to ask the programmer any question you wish regarding PHP, MySql, HTML, CSS, etc...
he programmer will then answer your question in another post, and reply to your comment here, so you will get an email notification.

Enjoy learning.
Manny E.
3wPHP Staff.

Filed under: General, PHP Basics 1 Comment
11Oct/100

Function to compare 2 string values

This function will compare 2 values,
make sure they are strings,
and make sure they are not empty

here is the code.

<?php
 
function str_compare($str1 , $str2)
{
	// we are converting the values to strings...
	// comment out the following 2 lines 
	// if you wish to check other value types.
	$str1 = (string)$str1;
	$str2 = (string)$str2;
 
	/// we are checking to see that both values are not empty.
	/// if one of the strings is empty, we return false.
	if (empty($str1) || empty($str2))
	{
		return false;
	}
	// the following line is the same like doing
	/*
	if($str1 === $str2)
	{
		$same = true;
	}
	else
	{
		$same = false;
	}
	*/
	$same = (($str1 === $str2)? true : false);
 
	return $same;
}
 
?>

enjoy learning, and please,
feel free to comment, or ask anything you want to know

11Oct/101

Welcome to 3wPHP.com!

Welcome to 3wPHP.com. This is our first post. We will be posting some code soon.

<?php
echo 'Welcome' ;
?>

Thank you
Manny E.
3wphp staff.

Filed under: General 1 Comment
11Oct/100

How to convert array to object

When you want to convert array to object, you will usually use a named(associative) array rather then numbered array...

Example:

<?php
 
$users = array();
$users['mark_long'] = array(
					'id' => 1 ,
					'firstname' => 'Mark',
					'lastname' => 'Long',
					'phone' => '718-555-5555'
				);
$users['john_smith'] = array(
					'id' => 2 ,
					'firstname' => 'John',
					'lastname' => 'Smith',
					'phone' => '718-555-5555'
				);
$users['sam_jonson'] = array(
					'id' => 3 ,
					'firstname' => 'Sam',
					'lastname' => 'Jonson',
					'phone' => '718-555-5555'
				);
foreach ($users as $username => $user_data)
{
	$user_data = (object)$user_data;
	//// now we can access he data using the object...
	//// so instead of using $user_data['firstname'], 
	//// we will be using $user_data->firstname ...
 
	echo 'User ID: '. $user_data->id .'<br />' ;
	echo 'Full Name: '.$user_data->firstname .' ' . $user_data->lastname .'<br />';
	echo 'Phone Number: '.$user_data->phone .'<br />' ;
 
}
 
?>

Well, here we just did something very simple...
Feel free to comment on this, or ask any questions you might have.
Thank you.

Filed under: PHP Basics No Comments