<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>PHP and Web Development Blog &#187; functions</title>
	<atom:link href="http://www.ebrueggeman.com/blog/tag/functions/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.ebrueggeman.com/blog</link>
	<description>Tips and Tricks for Web Developers</description>
	<lastBuildDate>Tue, 03 Aug 2010 17:21:32 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.6</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>PHP HTML Email Function</title>
		<link>http://www.ebrueggeman.com/blog/php-html-email-function/</link>
		<comments>http://www.ebrueggeman.com/blog/php-html-email-function/#comments</comments>
		<pubDate>Thu, 25 Sep 2008 01:38:39 +0000</pubDate>
		<dc:creator>Elliott Brueggeman</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[email]]></category>
		<category><![CDATA[functions]]></category>
		<category><![CDATA[html]]></category>

		<guid isPermaLink="false">http://www.ebrueggeman.com/blog/?p=40</guid>
		<description><![CDATA[PHP provides the mail() function for easy sending of mail from your PHP server. The problem is that sending HTML email (the most popular format for emails) is not easily done using native PHP functions. I&#8217;ve provided below a somewhat foolproof function that will allow you to send HTML emails.
Simply pass in a to address, [...]]]></description>
			<content:encoded><![CDATA[<p>PHP provides the mail() function for easy sending of mail from your PHP server. The problem is that sending HTML email (the most popular format for emails) is not easily done using native PHP functions. I&#8217;ve provided below a somewhat foolproof function that will allow you to send HTML emails.</p>
<p>Simply pass in a to address, from address, from name (which is used as a nickname in email clients instead of displaying the sender&#8217;s email address), subject, and a properly formatted HTML email message.</p>
<p>If you are sending to multiple recipients, you can pass the $to_email parameter in as an array of multiple email addresses.</p>
<h2>HTML Email Function</h2>

<div class="wp_syntax"><div class="code"><pre class="php php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> send_email <span style="color: #009900;">&#40;</span><span style="color: #000088;">$to_email</span><span style="color: #339933;">,</span> <span style="color: #000088;">$from_email</span><span style="color: #339933;">,</span> <span style="color: #000088;">$from_name</span><span style="color: #339933;">,</span> <span style="color: #000088;">$subject</span><span style="color: #339933;">,</span> <span style="color: #000088;">$msg</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #666666; font-style: italic;">//split up to email array, if given</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">is_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$to_email</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$to_email_string</span> <span style="color: #339933;">=</span> <span style="color: #990000;">implode</span><span style="color: #009900;">&#40;</span><span style="">', '</span><span style="color: #339933;">,</span> <span style="color: #000088;">$to_email</span><span style="color: #009900;">&#41;</span>;
	<span style="color: #009900;">&#125;</span>
	<span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$to_email_string</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$to_email</span>;
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">//Assemble headers</span>
	<span style="color: #000088;">$headers</span>  <span style="color: #339933;">=</span> <span style="">'MIME-Version: 1.0'</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>;
	<span style="color: #000088;">$headers</span> <span style="color: #339933;">.=</span> <span style="">'Content-type: text/html; charset=iso-8859-1'</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>;
	<span style="color: #000088;">$headers</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;From: $from_name &lt;$from_email&gt;&quot;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>;
&nbsp;
	<span style="color: #666666; font-style: italic;">//send via PHP's mail() function</span>
	<span style="color: #990000;">mail</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$to_email_string</span><span style="color: #339933;">,</span> <span style="color: #000088;">$subject</span><span style="color: #339933;">,</span> <span style="color: #000088;">$msg</span><span style="color: #339933;">,</span> <span style="color: #000088;">$headers</span><span style="color: #009900;">&#41;</span>;
<span style="color: #009900;">&#125;</span></pre></div></div>

<h2>Usage</h2>
<p>You call the function like this:</p>

<div class="wp_syntax"><div class="code"><pre class="php php" style="font-family:monospace;">send_email<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;me@gmail.com&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;roger@att.com&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;Roger&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;Hello There&quot;</span><span style="color: #339933;">,</span> 
  <span style="color: #0000ff;">&quot;&lt;html&gt;Hello There &lt;strong&gt;Bob&lt;/strong&gt;! How are you doing&lt;/html&gt;!&quot;</span><span style="color: #009900;">&#41;</span>;</pre></div></div>

<p>Or, alternately, you call it like this when sending to multiple email addresses:</p>

<div class="wp_syntax"><div class="code"><pre class="php php" style="font-family:monospace;">send_email<span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;me@gmail.com&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;sal@gmail.com&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;roger@att.com&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;Roger&quot;</span><span style="color: #339933;">,</span> 
  <span style="color: #0000ff;">&quot;Hello There&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;&lt;html&gt;Hello There &lt;strong&gt;Bob&lt;/strong&gt;! How are you doing&lt;/html&gt;!&quot;</span><span style="color: #009900;">&#41;</span>;</pre></div></div>

<h2>HTML Email Formatting</h2>
<p>Remember that formatting HTML for email is different than for webpages. Do not use a CSS file and do not declare CSS in the head of your document. Instead, use either inline CSS or &#8220;Old School&#8221; HTML like font tags for formatting. Also, don&#8217;t use JavaScript, as major email clients don&#8217;t support it. </p>
<p>Though it may seem obvious, remember that images and links within your HTML email need to be full, absolute paths to their world wide web accessible location. No relative links!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ebrueggeman.com/blog/php-html-email-function/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP trim_text() function &#8211; shorten text without cutting words in half</title>
		<link>http://www.ebrueggeman.com/blog/abbreviate-text-without-cutting-words-in-half/</link>
		<comments>http://www.ebrueggeman.com/blog/abbreviate-text-without-cutting-words-in-half/#comments</comments>
		<pubDate>Wed, 10 Sep 2008 03:25:02 +0000</pubDate>
		<dc:creator>Elliott Brueggeman</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[abbreviate]]></category>
		<category><![CDATA[functions]]></category>
		<category><![CDATA[trim]]></category>

		<guid isPermaLink="false">http://www.ebrueggeman.com/blog/?p=37</guid>
		<description><![CDATA[Here&#8217;s another PHP function I cannot live without &#8211; I add this into my standard PHP functions include on all projects. Often when displaying text, I am forced to abbreviate the text to a certain number of characters. You might jump in a decide to use substring() on your text to achieve this abbreviation, but [...]]]></description>
			<content:encoded><![CDATA[<p>Here&#8217;s another PHP function I cannot live without &#8211; I add this into my standard PHP functions include on all projects. Often when displaying text, I am forced to abbreviate the text to a certain number of characters. You might jump in a decide to use substring() on your text to achieve this abbreviation, but that can cause several problems. The most blatant is that you will often split the text right in the middle of a word. In addition, if there are any HTML tags in the text, they could get cut in the middle too, or have the closing tag left off completely, potentially breaking the display or exposing the remaining part of the tag.</p>
<p>As a solution to these problems, I have written a function that only trims on the last space before the number of characters you specify, so it will never cut words in half. Also, it strips out HTML tags before doing the character trim, preventing possible display issues. And, as a convenience it adds ellipses (the &#8230;) to all trimmed text, as a visual cue to the reader that the text has been abbreviated.</p>

<div class="wp_syntax"><div class="code"><pre class="php php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #0000ff; font-style: italic;">/**
 * trims text to a space then adds ellipses if desired
 * @param string $input text to trim
 * @param int $length in characters to trim to
 * @param bool $ellipses if ellipses (...) are to be added
 * @param bool $strip_html if html tags are to be stripped
 * @return string 
 */</span>
<span style="color: #000000; font-weight: bold;">function</span> trim_text<span style="color: #009900;">&#40;</span><span style="color: #000088;">$input</span><span style="color: #339933;">,</span> <span style="color: #000088;">$length</span><span style="color: #339933;">,</span> <span style="color: #000088;">$ellipses</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">true</span><span style="color: #339933;">,</span> <span style="color: #000088;">$strip_html</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #666666; font-style: italic;">//strip tags, if desired</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$strip_html</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$input</span> <span style="color: #339933;">=</span> <span style="color: #990000;">strip_tags</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$input</span><span style="color: #009900;">&#41;</span>;
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">//no need to trim, already shorter than trim length</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$input</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&lt;=</span> <span style="color: #000088;">$length</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #000088;">$input</span>;
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">//find last space within length</span>
	<span style="color: #000088;">$last_space</span> <span style="color: #339933;">=</span> <span style="color: #990000;">strrpos</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$input</span><span style="color: #339933;">,</span> <span style="color:#800080;">0</span><span style="color: #339933;">,</span> <span style="color: #000088;">$length</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="">' '</span><span style="color: #009900;">&#41;</span>;
	<span style="color: #000088;">$trimmed_text</span> <span style="color: #339933;">=</span> <span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$input</span><span style="color: #339933;">,</span> <span style="color:#800080;">0</span><span style="color: #339933;">,</span> <span style="color: #000088;">$last_space</span><span style="color: #009900;">&#41;</span>;
&nbsp;
	<span style="color: #666666; font-style: italic;">//add ellipses (...)</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$ellipses</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$trimmed_text</span> <span style="color: #339933;">.=</span> <span style="">'...'</span>;
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #b1b100;">return</span> <span style="color: #000088;">$trimmed_text</span>;
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.ebrueggeman.com/blog/abbreviate-text-without-cutting-words-in-half/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>PHP Get and Post Data Functions</title>
		<link>http://www.ebrueggeman.com/blog/php-get-post-functions/</link>
		<comments>http://www.ebrueggeman.com/blog/php-get-post-functions/#comments</comments>
		<pubDate>Sun, 09 Mar 2008 18:08:31 +0000</pubDate>
		<dc:creator>Elliott Brueggeman</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[functions]]></category>
		<category><![CDATA[GET]]></category>
		<category><![CDATA[includes]]></category>
		<category><![CDATA[POST]]></category>

		<guid isPermaLink="false">http://www.ebrueggeman.com/blog/php/php-get-post-functions/</guid>
		<description><![CDATA[MY Common PHP Includes
When I start a new application or website, I always start with the same framework of files. I have a series of include files that I include as necessary on pages (scripts) on the site. Stuff like site navigation, database connection, and core functions are some of the includes I use.
My functions [...]]]></description>
			<content:encoded><![CDATA[<h2>MY Common PHP Includes</h2>
<p>When I start a new application or website, I always start with the same framework of files. I have a series of include files that I include as necessary on pages (scripts) on the site. Stuff like site navigation, database connection, and core functions are some of the includes I use.</p>
<p>My functions include has all the functions that I&#8217;m confident are going to be used frequently enough on the site to warrant their inclusion (and subsequent declaration) in every script in the site. While most of the functions vary from site, because they relate to a specific feature or concept that the site incorporates, there are two functions that I always include.</p>
<h2>My Get and Post Functions</h2>
<p>All my sites and applications are dynamic, which means they receive input from the user using either the GET or POST method. If you incorporate forms into your site, you&#8217;re probably familiar with GET and POST and the differences between the two. One thing I noticed after some time programming was that handling data through GET and POST was done frequently and could be easily compartmentalized in two simple helper functions.</p>
<p>
Below is my implementation of these functions:
</p>

<div class="wp_syntax"><div class="code"><pre class="php php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000000; font-weight: bold;">function</span> getData<span style="color: #009900;">&#40;</span><span style="color: #000088;">$var</span><span style="color: #339933;">,</span> <span style="color: #000088;">$maxLength</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">99999</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000088;">$value</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">NULL</span>;
  <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$var</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$value</span> <span style="color: #339933;">=</span> <span style="color: #990000;">trim</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">htmlentities</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$var</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> ENT_QUOTES<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>;
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;</span> <span style="color: #000088;">$maxLength</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000088;">$value</span> <span style="color: #339933;">=</span> <span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$value</span><span style="color: #339933;">,</span> <span style="color:#800080;">0</span><span style="color: #339933;">,</span> <span style="color: #000088;">$maxLength</span><span style="color: #009900;">&#41;</span>;
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #b1b100;">return</span> <span style="color: #000088;">$value</span>;
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> postData<span style="color: #009900;">&#40;</span><span style="color: #000088;">$var</span><span style="color: #339933;">,</span> <span style="color: #000088;">$maxLength</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">99999</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000088;">$value</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">NULL</span>;
  <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$var</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$value</span> <span style="color: #339933;">=</span> <span style="color: #990000;">trim</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">htmlentities</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$var</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> ENT_QUOTES<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>;
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">&gt;</span><span style="color: #000088;">$maxLength</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000088;">$value</span> <span style="color: #339933;">=</span> <span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$value</span><span style="color: #339933;">,</span> <span style="color:#800080;">0</span><span style="color: #339933;">,</span> <span style="color: #000088;">$maxLength</span><span style="color: #009900;">&#41;</span>;
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #b1b100;">return</span> <span style="color: #000088;">$value</span>;
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>
These functions act as helper functions to retrieve the value that was sent to the script and available to PHP&#8217;s global $_GET and $_POST arrays. They also implement a few simple extra features. When calling either of these two functions, you can also supply a $maxLength parameter, which would be the maximum expected length of that variable. If anything is longer, then someone may be trying to take advantage of your website by sending data not in the way you intended. If the variable is longer than expected, the function still allows it, though it trims it to the length you are expecting. This can be helpful if you are inserting values into a database that has limits on the number of characters in a field. In addition, the function also puts the value through PHP&#8217;s htmlentities() function which will encode each character into their html equivalent, which I almost always need. This can also disarm any injected code that a malicious user may be sending to your script. Lastly, the function returns NULL when the expected variable has not been passed. This allows easy error handling when calling this function. Here is an example of doing error handling when retrieving a function:
</p>

<div class="wp_syntax"><div class="code"><pre class="php php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #666666; font-style: italic;">//Attempt to retrieve the value</span>
<span style="color: #000088;">$username</span> <span style="color: #339933;">=</span> getData<span style="color: #009900;">&#40;</span><span style="">'username'</span><span style="color: #009900;">&#41;</span>;
&nbsp;
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$username</span> <span style="color: #339933;">==</span> <span style="color: #000000; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #666666; font-style: italic;">//This value does not exist, do something to handle this situation</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #990000;">echo</span> <span style="">'Welcome '</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$username</span> <span style="color: #339933;">.</span> <span style="">'!'</span>;
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.ebrueggeman.com/blog/php-get-post-functions/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
