Geeks On PHP Episode 13

This week Stephen and I talk about strings and how they are accessed, manipulated and used in PHP.

We kick off the discussion talking about some differences between echo() and printf(). Below is an example of using both functions to populate a text field on a web form. We will start off with using echo():


.. input type="text" name="foo" id="foo"
[Inside PHP Block]
if(!empty($dbresult['foo']))
echo ' value="'.$dbresult['foo'].'" />';
else
echo ' value="" />';
?>

Here is the same end-result using printf():


... input type="text" name="foo" id="foo"
[Inside PHP Block]
printf(" value=\"%s\" ", !empty($dbresult['foo']) ? $aResults['foo'] : "");
?>

I tried to make the “argument” during the show that printf() allows for a lot more flexibility as well as resulting in slightly more compact code. Notice the usage of PHP’s ternary operator in the printf() example. It allows for a great deal of flexibility and power directly in-line w/ your output statement.

Regular Expressions were also a big topic of discussion this week and as promised here are some examples of commonly used RegEx’s in web application development:


$pattern_date = '#(0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.](19|20)\d\d#';
$pattern_email = '#^[a-zA-Z._]+@[a-zA-Z_]+?\.[a-zA-Z]{2,4}$#';
$pattern_email2 = '#^\w+([\.%-]\w+)*@\w+([\.-]\w+)*(\.\w{2,})+$#';
$pattern_phone = '#^(?:\([2-9]\d{2}\)\ ?|[2-9]\d{2}(?:\-?|\ ?))[2-9]\d{2}[- ]?\d{4}$#';
$pattern_time = '#(1[0-2]|[1-9]):([0-5][0-9])#';
$pattern_url = '#(http|https)\://[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,4}(/\S*)?$#';
$pattern_alphanum = "#^[a-zA-Z0-9\s]+$#";

To test matching on these patterns you can simply use the preg_match() function:

preg_match($pattern, $string, $matches);
if(!empty($matches))
    print_r($matches);

A couple of my favorite resources for regular expressions are:
The Online Regular Expression Testing Tool
The Regular Expression Library

Regular Expressions provide so much power and versatility when it comes to pattern matching and replacements that they just can’t be ignored. If you have questions about any of the above please let us know.

Follow both Stephen and Myself on Twitter and also subscribe via Tunes!

Enjoy the show and as always, stay geeky!

Audio clip: Adobe Flash Player (version 9 or above) is required to play this audio clip. Download the latest version here. You also need to have JavaScript enabled in your browser.

- Nicholas

Share

Read Users' Comments (1)

Geeks On PHP Episode 11

In this episode we talk about everything from image manipulation, generating charts & graphs, classes and frameworks and even home invasions.

The charting libraries I discuss are Fusion Charts and pChart, both are very good libraries and have great documentation on how to get started.

The following is a code snippet that I wrote which takes in an associative array, parses it out for a data label ($a[0]) and a data value ($a[1]) and generates a Fusion compatible XML file.

function fnGenerateXML($scaption, $sxaxis, $syaxis, $adata)
{
  if(empty($adata))
    die("Empty data set passed to function.");

  $sdata = "\n";

  foreach($adata as $arow)
    $sdata .= " \n";

  $sdata .= "";

  $sfile_xml = "./chart.xml";
  file_put_contents($sfile_xml, $sdata);
  return $sfile_xml;
}

The function is called in the following fashion:

$sfile_xml = fnGenerateXML("Caption", "X Lbl", "Y Lbl", $a);

The Fusion Chart is generated using the following method call:

echo renderChartHTML("./Column3D.swf", "$sfile_xml", "",
      "ID", 650, 650, false);

Here is some sample code I wrote to generate a graph using pChart:

// Dataset definition
$pData = new pData;
$pData->AddPoint($a1, "s1");
$pData->AddPoint($a2, "s2");
$pData->AddPoint($alabels, "lbl");
$pData->AddSerie("s1");
$pData->AddSerie("s2"); 

$pData->SetSerieName("s1lbl", "s1");
$pData->SetSerieName("s2lbl", "s2");
$pData->SetYAxisName("Ylbl");

$pData->SetAbsciseLabelSerie("lbl");

// Init graph
$pGraph = new pChart(800, 340);
$pGraph->setFontProperties("./tahoma.ttf",8);
$pGraph->setGraphArea(50,30,680,200);
$pGraph->drawFilledRoundedRectangle(7,7,693,240,5,240,240,240);
$pGraph->drawRoundedRectangle(5,5,695,240,5,260,230,230);
$pGraph->drawGraphArea(255,255,255,TRUE);
$pGraph->drawScale($pData->GetData(),
   $pData->GetDataDescription(),SCALE_ADDALL,150,150,150,TRUE,0,2,TRUE);
$pGraph->drawGrid(4,TRUE,230,230,230,50);

// Draw the 0 line
$pGraph->drawTreshold(0,143,55,72,TRUE,TRUE);

// Draw the graph
$pGraph->drawStackedBarGraph($pData->GetData(),
   $pData->GetDataDescription(),TRUE);

// Finish
$pGraph->drawLegend(500,40,$pData->GetDataDescription(),255,255,255);
$pGraph->drawTitle(225,22,"Title",0,0,0);
$pGraph->Stroke();

As you can see pChart requires a bit more setup to get going but both are great libraries for getting the job done. Another free option for charting/graphing that we didn’t discuss on the podcast is the Google Chart API.

Check both Stephen and Myself out on Twitter and also subscribe to us on iTunes!

Enjoy the show and as always, stay geeky!

Audio clip: Adobe Flash Player (version 9 or above) is required to play this audio clip. Download the latest version here. You also need to have JavaScript enabled in your browser.

- Nicholas

Share

Read Users' Comments (1)

New MasterCard & Discover Processing Requirements

MasterCard and Discover are modifying their rules concerning processing of debit, prepaid and gift cards. MasterCard’s rule changes went into effect 5/1/2010 and Discover’s rule changes went into effect on 4/16/2010. Merchants using Authorize.Net have until 6/30/2011 to comply with the new standards but since I am compliance hound I wanted to make these changes immediately.

Since I was reading up on all of the documentation and re-working my API code accordingly I figured I would share the changes with everyone in hopes of saving people a bit of time when they sit down to modify their own code.

So what’s new?
– Balance response transactions
– Partial authorization transactions
– Authorization reversals

So what does all that mean?
Transactions made on a gift/debt/etc card can now be split among multiple cards using partial authorization and the splitTenderId value. This value “links” the authorization request to subsequent transactions.

The new API request fields to pay attention to are ['x_allow_partial_Auth'] which is either true or false and ['x_split_tender_id'] which gets set to the original authorization request value.

The new relay response reply fields are as follows:
['x_account_number'] = last 4 digits of card
['x_card_type'] = string value indicating the type of card used
['x_split_tender_id'] = original authorization value
['x_prepaid_requested_amount'] = amount requested in original auto
['x_prepaid_balance_on_card'] = balance on the debit/prepaid card

The obvious things to note here (aside from the tender id) are the card balance and the requested amount. With these two values you can easily determine if the transaction can go through or if a subsequent transaction (w/ the sane tender id) has been requested.

New response codes to take note of are the following:
Reason: 295 Code: 4 — Amount was only partially approved
Reason: 296 Code: 3 — The splitTenderId is invalid.
Reason: 297 Code: 3 — A trans ID and a Split Tender ID can’t both be used in a single transaction request.
Reason: 298 Code: 3 — The splitTenderId has been completed (released or voided) and can not be associated with new transactions.

Here is what a sample relay repines looks like: (Taken from official documentation)

Hopefully this summary helps identify the changes that need to be made and why. All of this information can be found in the official documentation which is available at: http://www.authorize.net/support/pafaqs/

- Nicholas

Share

Read Users' Comments (1)

Geeks On PHP – Episode 9

After a long wait, Geeks on PHP – Episode 9 is finally out!  As I mentioned in a previous post we are now on a weekly recording schedule and will be releasing a new episode every Sunday evening unless otherwise specified.

In this exciting episode we discuss lots of information and things to think about *before* you start building your database or writing your application.  We also touch on SQL statements, database schema design and how they relate to your PHP applications.

Audio clip: Adobe Flash Player (version 9 or above) is required to play this audio clip. Download the latest version here. You also need to have JavaScript enabled in your browser.

Also, subscribe to us on iTunes and leave us your comments: [ Launch iTunes ]

Follow us on Twitter :
Nick : http://www.twitter.com/niczak
Stephen : http://www.twitter.com/darthweef

- Nicholas

Share

Read Users' Comments (0)

Geeks On PHP Is Alive!

Geeks On PHPHey all, I just want to give everybody an update on the Geeks On PHP podcast.  Stephen and I have arranged our schedules as such that we will be on a regular weekly recording schedule beginning NEXT WEEK.  This is very exciting news for us and for our listeners.  We apologize for the gaps between episodes in the past and the long gap between our last recording and the upcoming one.

Episode 9 will be focused on discussing different ways to utilize data from a database (agnostic discussion) with PHP.  We are set to record on Wednesday 4/28 and the show should be available on iTunes by the weekend.

We really want to thank all of our listeners for the feedback and support we received during our down time.  It means a lot to both of us and we look forward to getting back on track and bringing you the best PHP podcast the internet has to offer.

Sincerely,

- Nicholas Kreidberg

Share

Read Users' Comments (1)

Geeks on PHP — Update

geeksOnPHP

Hey all, I know the release schedule for the Geeks on PHP podcast has been erratic and unpredictable lately. We are currently in the process of revamping the format of the show including me taking over as host and the possibility of adding someone else as co-host. Stephen’s responsibilities with the New Brew Thursday podcast have increased exponentially which is the underlying reason behind the changes going on with Geeks on PHP.

I have a couple people in mind for co-hosts but would love to get feedback from our readers/listeners about what you would like to get out of the show. If any of you are interested in doing a guest spot please get in touch with me either by Email or on Twitter.

I will keep everybody updated as things progress but for now I just wanted to make people aware of what is going on and also that the show is not dead.

Thanks for listening and please don’t hesitate to comment, email or otherwise — your involvement is highly encouraged!

Sincerely,

- Nicholas Kreidberg

Share

Read Users' Comments (0)