Lil Josh

A tech blog about web development with PHP, MySQL, jQuery, CSS3 and various other programming languages.

Javascript Format Phone Number Function

July 2, 2013

This is a Javascript function to format any 10 digit phone number into the following format: (555) 555-5555

<script>
/*
* Created by http://liljosh.com
* Formats a phone number to be in (555) 555-5555 format
*/
function phoneFormat(phone) {
phone = phone.replace(/[^0-9]/g, '');
phone = phone.replace(/(\d{3})(\d{3})(\d{4})/, "($1) $2-$3");
return phone;
}
</script>

Example usage:

<div id="phoneNumber">555-555-5555</div>
<script>
var phone = $("#phoneNumber").text();
phone = phoneFormat(phone);
$("#phoneNumber").text(phone);
</script>

See demo:

http://jsfiddle.net/Q7GNX/

format function javascript jquery phone number

Introduction to PHP PDO

June 12, 2013

CONNECTION

//replace host, database, user, password
$dbh = new PDO("mysql:host=mysql.host.com;dbname=DATABASE", 'USER', 'PASSWORD'); 

SELECT

$stmt = $dbh->prepare("SELECT id, name FROM table");
$stmt->execute();
$result = $stmt->fetchAll();
foreach($result as $row) {
echo "$row[id]: $row[name]<br />";
}

SELECT WITH NAMED PARAMETER

$example = $_GET["example"];
$stmt = $dbh->prepare("SELECT id, name FROM table WHERE column = :value"); //value can be named whatever you'd like
$stmt->bindParam(":value", $example); //pass a variable to the named parameter
$stmt->execute();
$result = $stmt->fetchAll();
foreach($result as $row) {
echo "$row[id]: $row[name]<br />";
}

INSERT WITH NAMED PARAMETER

$firstname = $_GET["firstname"];
$lastname = $_GET["lastname"];
$stmt = $dbh->prepare("INSERT INTO `users` (`firstName`, `lastName`) VALUES (:firstname, :lastname)"); 
$stmt->bindParam(":firstname", $firstname);
$stmt->bindParam(":lastname", $lastname);
$stmt->execute();

UPDATE WITH NAMED PARAMETER

$date = date("Y-m-d H:i:s");
$id = $_GET["id"];
$stmt = $dbh->prepare('UPDATE `users` SET `lastLogin` = :lastLogin WHERE `id` = :id');
$stmt->bindParam(":lastLogin", $date);
$stmt->bindParam(":id", $id);
$stmt->execute();
cheatsheet code samples mysql PDO PHP queries quickstart Security

Setting up Subversion on Dreamweaver

December 24, 2012

This tutorial shows you how to setup Subversion on Dreamweaver using Google Code. I’m using CS6, but it likely applies to earlier versions.

  1. Create an account on Google Code: http://code.google.com
  2. Create a new project: http://code.google.com/hosting/createProject
    Create Project
  3. Click “Source” and you should see something similar to:
    Login Info
    You’ll need the following information.
    Server address: lil-josh-demo.googlecode.com (replace with yours)
    Repository Path: /svn/trunk
    Username: JoshFromIU@gmail.com (replace with yours)
    Password: You can find your password by clicking googlecode.com password.
  4. Open your site in Dreamweaver. From the dropdown click Site -> Manage Sites and double click your site to open it.
  5. Click the Version Control tab from the left.
  6. Enter your Google code information from step 3. For example:
    Dreamweaver Settings
  7. Click Test and then Save if everything worked.
  8. I’d recommend watching this video to get a quick overview of the Dreamweaver’s Subversion features: http://tv.adobe.com/watch/learn-dreamweaver-cs5/working-with-subversion-and-dreamweaver/ (skip to 1:40)

I spent a lot of time trying to figure out how to setup a subversion server, and found that Google Code was the easiest solution. If you have any feedback or questions feel free to leave a comment. Thank you and enjoy!

CS6 dreamweaver google code subversion svn

Replace Function Workaround in XSLT 1.0

December 20, 2012

I came across a problem where I wanted to display HTML code on a page using XSLT, but I needed to replace all ‘<' and '>‘ characters with the HTML entity ‘<‘ and ‘>’. With XSLT 2.0 this would have been super simple using the built-in replace() function. However, I was stuck with XSLT 1.0. My solution was to use jQuery to select the element I wanted to display and then replace all the mentioned characters within it. Here’s an example:

$(document).ready(function() {
var body = $("body").html();
body = body.replace(/</g, "&lt;");
body = body.replace(/>/g, "&gt;");
$("body").html(body);
});
1.0 2.0 jquery replace XSLT