Sunday, March 1, 2009

Php extract method

"extract()" Method



Experimentally I found that calling extract() also shows the number of keys if the key is set and is not numeric ! Maybe there was a better definition than mine  . Please have a look to this scripts :
<?PHP
$var
["i"] = "a"
;
$var["j"] = "b"
;
$var["k"] = 1
;
echo
extract($var);
// returns 3
?>

<?PHP
$var2
["i"] = "a"
;
$var2[2] = "b"
;
$var2[] = 1
;
echo
extract($var2);
// returns 1
?>


 

(Arash Moslehi)

Sunday, February 8, 2009

Sending Email with Attachment

The last variation that we will consider is email with attachments. To send an email with attachment we need to use the multipart/mixed MIME type that specifies that mixed types will be included in the email. Moreover, we want to use multipart/alternative MIME type to send both plain-text and HTML version of the email. Have a look at the example:
<?php
//define the receiver of the email
$to = 'youraddress@example.com';
//define the subject of the email
$subject = 'Test email with attachment';
//create a boundary string. It must be unique
//so we use the MD5 algorithm to generate a random hash

$random_hash = md5(date('r', time()));
//define the headers we want passed. Note that they are separated with \r\n
$headers = "From: webmaster@example.com\r\nReply-To: webmaster@example.com";
//add boundary string and mime type specification
$headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\"";
//read the atachment file contents into a string,
//encode it with MIME base64,
//and split it into smaller chunks

$attachment = chunk_split(base64_encode(file_get_contents('attachment.zip')));
//define the body of the message.
ob_start(); //Turn on output buffering
?>
--PHP-mixed-<?php echo $random_hash; ?> 
Content-Type: multipart/alternative; boundary="PHP-alt-<?php echo $random_hash; ?>"

--PHP-alt-<?php echo $random_hash; ?> 
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit


Hello World!!!
This is simple text email message.


--PHP-alt-<?php echo $random_hash; ?> 
Content-Type: text/html; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit


<h2>Hello World!</h2>
<p>This is something with <b>HTML</b> formatting.</p>


--PHP-alt-<?php echo $random_hash; ?>--

--PHP-mixed-<?php echo $random_hash; ?> 
Content-Type: application/zip; name="attachment.zip" 
Content-Transfer-Encoding: base64 
Content-Disposition: attachment 


<?php echo $attachment; ?>
--PHP-mixed-<?php echo $random_hash; ?>--


<?php
//copy current buffer contents into $message variable and delete current output buffer
$message = ob_get_clean();
//send the email
$mail_sent = @mail( $to, $subject, $message, $headers );
//if the message is sent successfully print "Mail sent". Otherwise print "Mail failed"
echo $mail_sent ? "Mail sent" : "Mail failed";
?>


As you can see, sending an email with attachment is easy to accomplish. In the preceding example we have multipart/mixed MIME type, and inside it we have multipart/alternative MIME type that specifies two versions of the email. To include an attachment to our message, we read the data from the specified file into a string, encode it with base64,  split it in smaller chunks to make sure that it matches the MIME specifications and then include it as an attachment.

Sending HTML Email

The next step is to examine how to send HTML email. However, some mail clients cannot understand HTML emails. Therefore it is best to send any HTML email using a multipart construction, where one part contains a plain-text version of the email and the other part is HTML. If your customers have HTML email turned off, they will still get a nice email, even if they don't get all of the HTML markup. Have a look at the example:
<?php
//define the receiver of the email
$to = 'youraddress@example.com';
//define the subject of the email
$subject = 'Test HTML email';
//create a boundary string. It must be unique
//so we use the MD5 algorithm to generate a random hash
$random_hash = md5(date('r', time()));
//define the headers we want passed. Note that they are separated with \r\n
$headers = "From: webmaster@example.com\r\nReply-To: webmaster@example.com";
//add boundary string and mime type specification
$headers .= "\r\nContent-Type: multipart/alternative; boundary=\"PHP-alt-".$random_hash."\"";
//define the body of the message.
ob_start(); //Turn on output buffering
?>
--PHP-alt-<?php echo $random_hash; ?> 
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit

Hello World!!! 
This is simple text email message. 


--PHP-alt-<?php echo $random_hash; ?> 
Content-Type: text/html; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit


<h2>Hello World!</h2>
<p>This is something with <b>HTML</b> formatting.</p>


--PHP-alt-<?php echo $random_hash; ?>--
<?
//copy current buffer contents into $message variable and delete current output buffer
$message = ob_get_clean();
//send the email
$mail_sent = @mail( $to, $subject, $message, $headers );
//if the message is sent successfully print "Mail sent". Otherwise print "Mail failed" 
echo $mail_sent ? "Mail sent" : "Mail failed";
?>


In the preceding example we add one additional header of Content-type:multipart/alternative and boundary string that marks the different areas of the email. Note that the content type of the message itself is sent as a mail header, while the content types of the individual parts of the message are embedded in the message itself. This way, mail clients can decide which part of the message they want to display.

Sending a Simple Text Email

At first let's consider how to send a simple text email messages. PHP includes the mail() function for sending email, which takes three basic and two optional parameters. These parameters are, in order, the email address to send to, the subject of the email, the message to be sent, additional headers you want to include and finally an additional parameter to the Sendmail program. The mail() function returns True if the message is sent successfully and False otherwise. Have a look at the example:
<?php
//define the receiver of the email
$to = 'youraddress@example.com';
//define the subject of the email
$subject = 'Test email';
//define the message to be sent. Each line should be separated with \n
$message = "Hello World!\n\nThis is my first mail.";
//define the headers we want passed. Note that they are separated with \r\n
$headers = "From: webmaster@example.com\r\nReply-To: webmaster@example.com";
//send the email
$mail_sent = @mail( $to, $subject, $message, $headers );
//if the message is sent successfully print "Mail sent". Otherwise print "Mail failed" 
echo $mail_sent ? "Mail sent" : "Mail failed";
?>

As you can see, it very easy to send an email. You can add more receivers by either adding their addresses, comma separated, to the $to variable, or by adding cc: or bcc: headers. If you don't receive the test mail, you have probably installed PHP incorrectly, or may not have permission to send emails.

Friday, February 6, 2009

how to remove html tags from text in php

if you are reading my post, its probably because you want to remove html tags from your posts or forums or whatever.

when i first started to do this, i wanted to remove all the html tags from a strin i had in my posts. this is an example:
<?
$string = "<h1>BIG HTML TAGS</h1>";
?>

ok as you can see, this will output:

BIG HTML TAGS


but i dont want that, i want to stop and prevent html from being posted on my forums. i tried using htmlspecialchars() and htmlentities() but none did what i wanted.

but finally i found it, the function is called strip_tags()

so if i wanted to remove the <h1> tags from my string i would just simply do this:
<?
$string = "<h1>BIG HTML TAGS</h1>";
$string = strip_tags($string);
?>

and this would be the ouput:
BIG HTML TAGS

Sunday, August 17, 2008

X-Cart – ecommerce shopping cart software

X-Cart – ecommerce shopping cart software:



  • X-Cart shopping cart software is based on PHP Smarty templates, this makes it very flexible and easy to modify. The data is stored in MySQL database.

  • W3C XHTML 1.0 compliance of X-Cart storefront out of the box.

  • You receive a complete source code and SQL tables for MySQL database to make them extended or customized easily.

  • X-Cart is a turnkey package. Installation is handled by web-based step-by-step wizard to make the system running immediately after receiving it.

  • X-Cart is search engine friendly. It has integrated HTML catalog functionality to gain benefits of dynamic content and static HTML preferred by search engines.

  • We bundle our software with 24x7 free technical support for beginners. Our experts are always ready to answer questions, even handle installation & configuration tasks.

  • We also offer custom programming services. Every client can get a storefront with a unique look. We can customize the software to completely fit the structure of your business.

  • Additional software licenses come at discounted rates.


More advantages:


Robust feature rich ecommerce shopping cart software is a key in online business success. Its potential can be multiplied many times if accompanied by coherent resources & services to give a hand on the way to online success not leaving you alone when operating live ecommerce website:





  • Moneyback guarantee


    We offer unconditional 30-day money back guarantee as a sign of our products quality to ensure best customers' experience in any case. Simply request a refund within 30 days since purchase to have all the money back.


  • Community forums


    The value of help provided by community forums should not be underestimated. Share experience with thousands of real business owners to find answers & hints to be successful. The forums are intentionally made available for clients only to ensure community forums are the meeting point of people who actually are in the business.


  • Responsive support


    Free technical support offered with each license guarantees successful project launching. We designed our software to minimize the need of this, so free technical support is a warranty card to keep you confident.


  • Partnership


    Regardless of whether you are a web hosting provider, developer, payment gateway or ISP, we can setup a partner relationship profitable for both parties. Read about our Affiliate program and see Reseller prices.


Website: http://www.x-cart.com/

Tuesday, July 29, 2008

PHP Email Address Validation

<?Php
function check_email_address($email) {
// First, we check that there's one @ symbol, and that the lengths are right
if (!ereg("^[^@]{1,64}@[^@]{1,255}$", $email)) {
// Email invalid because wrong number of characters in one section, or wrong number of @ symbols.
return false;
}
// Split it into sections to make life easier
$email_array = explode("@", $email);
$local_array = explode(".", $email_array[0]);
for ($i = 0; $i < sizeof($local_array); $i++) {
if (!ereg("^(([A-Za-z0-9!#$%&'*+/=?^_`{|}~-][A-Za-z0-9!#$%&'*+/=?^_`{|}~\.-]{0,63})|(\"[^(\\|\")]{0,62}\"))$", $local_array[$i])) {
return false;
}
}
if (!ereg("^\[?[0-9\.]+\]?$", $email_array[1])) { // Check if domain is IP. If not, it should be valid domain name
$domain_array = explode(".", $email_array[1]);
if (sizeof($domain_array) < 2) {
return false; // Not enough parts to domain
}
for ($i = 0; $i < sizeof($domain_array); $i++) {
if (!ereg("^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|([A-Za-z0-9]+))$", $domain_array[$i])) {
return false;
}
}
}
return true;
}
?>


Using the function above is relatively simple, as you can see:

<?Php
if (check_email_address($email)) {
    echo $email . ' is a valid email address.';
} else {
    echo $email . ' is not a valid email address.';
}
?>