Character set encoding matters as many language characters are not part of the standard ASCII character set. Instructions such as strlen will not behave the way you think when using it with extended characters such as €, ©, ®, à, ë, and many others. And languages such as Mandarin and Russian require a different approach when working with strings. In this episode, let’s talk about why the encoding matters and how the character set is really represented in the computer. Hint: Everything in the computer breaks down into binary, i.e. ones and zeros. Simple string instructions like strlen use bytes. […]
Labs
Labs are hands-on coding projects that you build along with Tonya as she explains the code, concepts, and thought processes behind it. You can use the labs to further your code knowledge or to use right in your projects. Each lab ties into the Docx to ensure you have the information you need.
Each lab is designed to further your understanding and mastery of code. You learn more about how to think about its construction, quality, maintainability, programmatic and logical thought, and problem-solving. While you may be building a specific thing, Tonya presents the why of it to make it adaptable far beyond that specific implementation, thereby giving you the means to make it your own, in any context.
Lab Introduction
Let’s introduce you to what you’ll be doing in this hands-on lab. You will be building a plugin that houses different utility functions. In this lab, you’ll be working on string processing and checking functions. You’ll build reusable functions that you can use on every project…to save you time.
Doing More With PHP String Processing
There are many times when we need to more complex processing with strings. PHP provides us with the instructions we need to verify and manipulate strings. In this hands-on lab, you will continue learning about processing strings in PHP.
Specifying Which Argument in a Formatted String
What if you need to repeat one of the arguments within the formatted string? You could repeat the argument in the list again like this: But then that’s a WET pattern, as you are repeating code. Instead, PHP lets you specify which argument you want for each of the placeholders. Resources sprintf – in Docx sprintf – PHP Manual printf – PHP Manual Type Casting
Embedding Complex Variables
In the last episode, I had you wrap the embedded variable within curly braces. Why? Readability. Yes, you can omit the curly braces with a simple variable embed. However, to me, it’s less readable. Therefore, I advocate always using the curly braces like this: “My name is {$first_name}.” How do you embed an element from an associative array or object? These embeds are known as complex variables. For these, you must wrap the variable within curly braces. In this episode, you will walk through embedding an associative array element within a string.
Wrap it Up
Let’s review what you learned in this lab. Congratulations for completing this lab!
Splitting a String into An Array
Truncate by Words
Truncating or trimming the length of a string down to a set limit is a task that we do often. Right? You have a blog page and you want to show a snippet of the content, but only 200 or 300 words and not the whole 2000 word articles. WordPress has a function built in that you can use called wp_trim_words(). I have one that I’ve used which is inspired by Laravel and Symfony. It’s part of my Fulcrum plugin that I use on all of my projects. Genesis has one built into too called genesis_truncate_phrase(). There are multiple ways […]
Truncate by Characters
There are times when you want to truncate a string by a certain number of characters. You are limiting the string to say 100 or 200 characters. How do you do that? In this episode, you and I will build the utility function that will handle it for you. Then you just call it whenever you need that feature in your theme or plugin. Resources In this utility function, you used the following instructions: mb_strwidth mb_strimwidth rtrim
Check if String Ends With a Character or Substring – Part 1
Your next challenge is to figure out how to check for a character or substring in a given haystack (source string). Just like with the “starts with” functionality, there are a couple of different implementations. In this two-part series, you’ll explore: Implementation #1: Using a string position implementation mb_strpos by leveraging the offset argument to start exactly where we want. Implementation #2: Use the same strategy as we did with “starts with”. We’ll walk through both implementations, talk about how they work and process, and then explore which is the better solution and why.