Reading .docx files using PHP

I had to process a lot of Word .docx files into readable content for use in a searchable database.  Docx files are basically xml files in a zipfile container (as described by wikipedia).  Here is my solution, it’s pretty straight forward.  Just pass in the server file path to the read_docx() function and it will return the text from that file.

 

Validate email using a RegEx pattern

Validating Email Addresses is one of the more elusive patterns to define, but this pattern will match 99.99% of all email addresses in actual use today.

A further change you could make is to allow any two-letter country code top level domain and only specific generic top level domains. This regex filters dummy email addresses like asdf@adsf.adsf. You will need to update it as new top-level domains are added.

Example using PHP:

This function will return true/false if the email values passed into it validates against the pattern.

 

Split Mail Address into it’s parts using RegEx pattern

I develop a lot of data parsers and continuously run into composite mailing addresses that require to be split into their meta collections. The following Regex pattern will reformat these two style addresses into XML tag elements.

SAMPLE ADDRESS DATA:

1200 Ocean Rd., Narragansett, RI 02882
1200 Ocean Rd., Narragansett, RI 02882-1234

FIND PATTERN USING REGEX:

REPLACE WITH XML FORMAT:

OUTPUT:

<address>1200 Ocean Rd.</address>
<city>Narragansett</city>
<state>RI</state>
<zip>02882</zip>

Granting Access to a Single S3 Bucket Using Amazon IAM

If you’ve ever used Amazon’s AWS console then you probably know that though sometimes it can be clunky, it has a ton of functionality for interacting with the various AWS services. So when I needed to give one of my coworkers access to one of our S3 buckets, I immediately investigated the laziest option: figuring out how they could login to the S3 console and use that to manage the bucket.

The S3 console is pretty great. Uploading, downloading, creating folders, managing permissions, even copying and pasting buckets between files is a snap. If I could figure this out, I would save myself all the work of setting up S3Fox or even worse, writing an interface from scratch.

After some trial and error, success! I’ve written a quick guideline on how to do this below.

1. Login to the IAM AWS console

Login here as the owner of the AWS account. Click the IAM tab.

2. Create an account alias

This step is optional, but it gives you a nice login URL for your users. Add an account alias in the AWS Account Alias section of the IAM console. Then, your login URL will be youralias.signin.aws.amazon.com.

If you don’t do this, your login page URL will be a bunch of random numbers.

3. Create a new group or a new user

With IAM, you can create a group that has certain permissions, and then assign users to that group. Or, you can just create users piecemeal, but then you can’t reuse permissions.

If you want a group, create it first. Then create a user and assign it to that group.

4. Set a password for the new user

Click the new user you’ve created and then click the Security Credentials tab. On that page, you can click Manage Password to add a password for your user. Without a password, the user won’t be able to login to the AWS console.

Make sure your user knows to use the login page from step #2 in order to login — they can’t use the regular AWS login page.

You’ll notice your user also has an AWS access key created: API clients using this key will have the same permissions as the user would in the AWS console.

5. Add permissions for your user

Permissions are added either on the group the user is in, or if you decided not to create a group, the user account itself.

Click the user or group, then click the Permissions tab. Here you can see which permissions policies are currently attached to the group or user. Click the Attach Policy button. You’ll get a pop-up where you can Manage User Permissions. Here you can select a prerolled policy, use the Policy Generator, or just paste in a custom policy.

There are two permissions that need to be added in order for your user to be able to login, see the bucket list in the S3 console, and manage the one bucket you’ve assigned.

To manage the bucket, you need to grant the s3:* action for the bucket you designate. AWS policies designate resources by their Amazon Resource Name, or ARN and for S3 buckets, they look like: arn:aws:s3:::bucket-name-here. So to grant your user full access to your bucket, you’d paste the policy:

Now, you would think that this would be enough to enable the user to use the S3 console to manage the bucket, but you’d be wrong. Turns out the user needs one more permission to do the initial listing of the buckets in order to be able to select a bucket, and its called s3:ListAllMyBuckets. You need to add that permissions too, and it looks like this:

6. Done!

You’re done. Give the user their credentials and the login page, and then bask in the glory of laziness.

Update:

Here is he full merged JSON object for those having trouble merging them together:
 

750 Free icons from Google for Material Design

action-icons

Google Design are open-sourcing 750 glyphs as part of the Material Design system icons pack. The system icons contain icons commonly used across different apps, such as icons used for media playback, communication, content editing, connectivity, and so on. They’re equally useful when building for the web, Android or iOS.

Check them out: https://github.com/google/material-design-icons/releases/tag/1.0.0

Bootstrap Responsive Video Embeds

Responsive videos seem to be an after-thought for many websites. While text and images are easy to resize to mobile and tablet screens, videos have always required extra work. Even popular video sharing sites like Youtube.com still don’t have an easy “responsive” option and require specifying a video dimension for embedding.

bs-responsive-video

Luckily, the 3.2+ version of Bootstrap solves that with a few simple classes you apply. To use them, wrap the <iframe> code with a div and give it the class of embed-responsive and either embed-responsive-16×9 (for widescreen) or embed-responsive-4by3 (if your videos are stuck in the 1980s format).

The embed-responsive-item part is optional. Since the only requirement is the outer wrapper for the video, it’s easy to globally apply this previously embedded videos, saving a ton of work with legacy content. This little snippet of jquery using .wrap() will wrap any iframe for you.

If you’re upgrading from an older version of Bootstrap and compiled your CSS from the LESS source files, you’ll need to include the responsive-embed.less file in your bootstrap.less file so the new styles are available.

Top 10 Best AngularJS Templates in 2015

top-10-templates-featured-750x410

As 2015 comes to a close, it seems appropriate to round out a list of the best AngularJS templates that I’ve seen this year.

Whether you’re looking to build a dashboard application, a portfolio application, or you are merely looking for a responsive AngularJS template to build your application on, there is sure to be a template suited for your needs. Here are our top 10 best AngularJS templates this year.

There are many fantastic templates available on the web, and it is difficult to decide which is the best AngularJS template available. When looking for a suitable template, there are many decisions that you have to weigh: responsiveness, quality of code, design, price, etc. Depending on what web app you are building, the best template for you might be different from that of another web application.

This list contains 10 of my favorite templates, and I am are sure that there are many more quality templates available.

1. Metronic

metronic-dashboard-template

Metronic is a responsive and multipurpose theme powered through the Twitter Bootstrap and AngularJS frameworks. It is a flexible theme that can be used for many different kinds of applications.

Metronic has a beautifully designed flat UI, and works on all major web browsers and on mobile devices. Metronic currently comes with 6 complete admin themes. Metronic’s beautiful UI and flexibility makes it one of our all time favorite AngularJS themes, and is definitely one of the best AngularJS templates available.

2. Xenon

xenon-dashboard-theme

Xenon is a lightweight responsive dashboard and admin template built with Bootstrap. Xenon contains plenty of UI components, layout variants and themes that make up a total of 136 HTML files. Talk about value for money!

Despite its large number of files, Xenon practices neat organization of files, clean code, and easily expandable features. Xenon is responsive and retina ready and works perfectly with mobile devices. It is one of the best AngularJS templates available that has almost everything, and is sure to save you plenty of development time when working on a large project.

3. INSPINIA

inspinia-angularjs-template

INSPINIA is a premium responsive admin template built with Bootstrap 3, HTML 5, and CSS 3. INSPINIA comes with a huge collection of reusable UI components and features a beautifully designed flat UI. It can be used in a wide variety of applications such as admin panels, application backends, content management systems, and more.

INSPINIA not only supports AngularJS, but also many other frameworks such as Ruby on Rails and Meteor, and comes with a seed project for each. It is an easily reusable template that promises continuous free updates, making it one of the most versatile and best AngularJS templates for use in almost any application.

4. Admin Box

admin-box-dashboard-template

Admin Box is a beautifully presented AngularJS dashboard template. It is powered by SASS and Grunt for easy development, testing and production.

Admin Box comes packaged with a suite of features, such as form validation and customization, vector maps, and many more. The Admin Box template also uses SASS files, which makes it extremely easy to customize it to your needs, making it a fantastic template to use for web development.

5. Material

material-responsive-template

Material is a powerful, lightweight, fully responsive AngularJS template. Material is inspired by Google Material Design and sports a gorgeous UI powered by Bootstrap 3.

Material comes with a variety of UI components and form components such as a date picker, color picker, toggle elements, and retina display compatible icons and images. All the components used in Material are completely customized to match Material Design, making it a beautifully cohesive template to start your next application on.

6. Pages

pages-responsive-template

Pages is not just a dashboard template, but a multi-purpose, carefully designed UI framework. It is crafted with special attention to detail. It comes with a large variety of application templates such as a calendar, social dashboard, email application, and many other UI features such as a navigation menu.

Pages is a responsive AngularJS theme with a beautifully designed UI. Its large number of potential uses and promises of free updates makes it one of the best AngularJS templates available. It’s a steal at $23 on the ThemeForest market.

7. Angle

angle-dashboard-theme

Angle is a web dashboard app based on Bootstrap and AngularJS. This dashboard template contains many ideal components for use in your next dashboard page or web project.

Angle contains two dashboard templates: for use in AngularJS as well as jQuery, allowing you to utilise this dashboard template in non-AngularJS apps as well. It is a great responsive theme with many useful components, making it one of the best AngularJS templates to build your dashboard project with.

8. Angulr

anglr-angularjs-template

Angulr is a web admin theme powered by Bootstrap 3 and AngularJS. Angulr features nested views, routing and lazy loading making it suitable for large projects. Angulr is fully responsive and works on mobile devices as well as across browsers.

Angulr is a very comprehensive template that can be used for a wide variety of applications. It also comes with a single page music application template, perfect for rapid prototyping of your next music application.

9. SmartAdmin

smart-admin-angularjs-template

SmartAdmin is a responsive web application template that contains working variations for multiple web frameworks, including AngularJS. It makes use of Grunt for working tasks and uses LESS and SASS CSS for easy customization, allowing you to easily modify the template for use in your web applications.

SmartAdmin comes with over 50 customized plugins and promises free lifetime updates. It claims to be a “chameleon” template that adapts to your needs, and it is built using modern technologies. It is fully responsive and is one of the best AngularJS templates that also caters to other frameworks, should you choose to use it as such.

10. Triangular

triangular-angularjs-template

Triangular is a unique Material Design AngularJS dashboard template. It is built from the ground up using Google’s Angular Material Design project. It claims to be the only fully Material Design AngularJS template around.

Triangular is a well organized template that uses best practices for front-end development. It comes bundled with 4 premium packages that include tons of Material Design resources, such as avatars, backgrounds, and fonts, making it one of the best AngularJS templates that makes full use of Material Design.

 

Validating String is UTF-8 using PHP

First, note that it is not possible to detect whether text belongs to a specific undesired encoding. You can only check whether a string is valid in a given encoding.

You can make use of the UTF-8 validity check that is available in preg_match since PHP 4.3.5. It will return false (with no additional information) if an invalid string is given:

Another possibility is mb_check_encoding:

Another function you can use is mb_detect_encoding:

It’s important to set the strict parameter to true.

Additionally, iconv allows you to change/drop invalid sequences on the fly. (However, if iconv encounters such a sequence, it generates a notification; this behavior cannot be changed.)

You can use @ and check the length of the return string:
Check the examples on the iconv manual page as well.

Comprehensive .htaccess script for Codeignighter

Having worked with CodeIgnighter for many years, I’ve crafted the following the following HTACCESS script to meet most CI needs.

 

There’s No Such Thing as Constructive Criticism

Here’s a question guaranteed to make your stomach lurch: “Would you mind if I gave you some feedback?”

What that means is “Would you mind if I gave you some negative feedback, wrapped in the guise of constructive criticism, whether you want it or not?”

The problem with criticism is that it challenges our sense of value. Criticism implies judgment, and we all recoil from feeling judged. Threats to our esteem in the eyes of others are so potent they can feel like threats to our very survival.

The conundrum is that feedback is necessary. It’s the primary means by which we learn and grow. So what’s the best way to deliver it in a way that it provides the greatest value — meaning the recipient truly absorbs and acts on it?

There are three critical behaviors, I believe, and they’re each grounded in the recognition that what we say is often less important than how we say it.

1. The first mistake we often make is giving feedback when we feel that our value is at risk. That’s a recipe for disaster, and it happens far more commonly than we think or are aware.

If we’re feeling threatened or diminished by another person’s perceived shortcomings, providing “constructive criticism” becomes secondary to getting our value back. We’re more likely to be reactive, insensitive and even hurtful.

If it’s about us, it’s not truly about them. Anytime we provide feedback with the goal of getting someone to meet better our needs, rather than being responsive to theirs; it’s unlikely to prompt the desired outcome.

A classic example is a parent who confuses his worth with his child’s performance and reacts to the child’s missteps with harshness and judgment rather than sensitivity and compassion.

2. The second mistake we make in giving feedback is failing to hold the other person’s value in the process. Even the most positive criticism will, more often than not, prompt us to feel our value is at risk, and under attack.

When that happens, the primal impulse is to defend ourselves. The more the person you are criticizing feels compelled to defend her value, the less capable she becomes of absorbing what she’s hearing.

I once had a subordinate who was highly competent and detail-driven and rarely made mistakes. Partly this grew out of her fierce perfectionism and her tremendous fear of the consequences of being wrong.

Her natural instinct was to deny responsibility for any misstep. When I felt the need to bring one to her attention, I learned it was crucial to begin by reassuring her that I cared about her and that I had continuing confidence in her abilities. Only then could she take in what I was saying.

When you’re inclined to offer accurate feedback, pause and ask yourself first how you’d feel if someone gave you that feedback. If you would feel uncomfortable or defensive, assume anyone else would too.

3. The third mistake we make is to believe that that we’re right about whatever it is we’re inclined to say. Like lawyers, we take a series of facts and weave them together into a story that supports and justifies the case we’re seeking to make.

The problem is that our stories aren’t necessarily true. They’re simply one interpretation of the facts. It makes much more sense to think about offering feedback in a spirit of little exploration rather than declaration, dialogue rather than monolog, curiosity rather than certainty. Humility is the recognition that we don’t know, even when we think we know.

Ultimately, we’d be better off eliminating concepts like “feedback” and “constructive criticism” from our lexicons altogether. They’re polarizing and mostly destructive. We need to think of these interchanges instead as opportunities for honest inquiry and genuine learning.

“Here’s the story I’m telling myself about what just happened,” we might say. “Have I got that right, or am I missing something?”

That’s what I intend to say the next time I’m inclined to ask someone, “Would you mind if I gave you some feedback?”