In this episode, we’ll take on the challenge of how to test a string to see if it starts with “http://”. You’ll discover there are multiple ways to do this check. Then we’ll create a utility function called starts_with and put that into our new Site Utilities plugin. Regex is not needed here. In this episode, I show you two different approaches to checking if a string begins with a substring: Check its position using strpos Check the beginning of the string to a specific length using mb_substr and mb_strlen
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.
Joining Array Elements into a String
Get the String’s Length
There are times when you need to know the length of a string. For example, if you are checking for a string and you want to know if it’s at the end of another string, then you need to know the length of what you are looking for first. That’s a good example. Let’s get you started learning about string lengths, how it’s processed (character vs. byte count), and character encoding. In this episode, you’ll work with the PHP instruction strlen. This instruction counts the number of bytes in the string, not the number of characters. For now, don’t worry […]
Stripping out Characters or Entities
Frequently, you need to strip off unwanted characters or entities from the start and end of a string. For example, form field submissions, such as custom fields, typically require you to clean them up before processing. When you pull data from the database, many times, you need to clean it up before you process and then render it out to the browser. In this episode, you’ll experiment with and learn about trim. You’ll pass character masks to it to see how you can control what is stripped off.
Replacing Substrings
In the last episode, you checked if a substring is present in the subject string. In this one, let’s replace it with another substring if it is.
Has Substring
There are times when you check if a substring exists within a string. For example, maybe you want to check for a class attribute or if the read more link text says “Continue reading.” PHP gives you do this using strpos. This PHP instruction checks for the substring or character (needle) that you want within the target string (haystack). If it finds it, then it returns its character position to you; else, it returns false. In this episode, you will search the content as well as the read more link. I’ll show you how it works and when to use […]
Concatenating and Assigning Shorthand
Often we need to build strings incrementally. A good example is with building an output HTML string pattern or even a message. You build part of it, do some processing, and then keep appending to the same string and assigning it back to the variable. PHP gives you a shortcode operator for this process that turns: $message = $message . ‘Welcome ‘ . $first_name; into: $message .= ‘Welcome ‘ . $first_name;. Both of these lines of code are processed the same way by the PHP interpreter. First, the strings are concatenated. Then the assignment is made to the variable $message.
Concatenating Strings with a Dot
PHP gives multiple ways to build strings. You previously learned about embedding variables into the string. In this episode, you will learn about the concatenating operator, which is a dot .. PHP takes the left string and smooshes it together with the string on the right side of the dot. Think of it as smooshing the two strings together. This method gives you the ability to break the code out of the string literal. Then you get the ability to call functions too. When putting together multiple string parts in one line of code, I want you to stop and […]
Embedding Variables in a String
PHP provides the means for us to build dynamic strings, i.e. strings that have varying strings within them. One method is to embed a variable directly into the string. When PHP comes to a single quote, it handles the string within the quotes as a string literal. There is no processing that happens. Rather, it treats it as a hard-coded string. Therefore, if you wrote echo ‘My name is {$first_name}.’, the entire string would be rendered out to the browser just as you wrote it. The variable would not be processed.
Lab Introduction
In this lab, you and I are going to work with strings. You’ll learn about the why, when, what, and how of string building and processing with PHP. You’ll learn about embedding variables to make a string dynamic, concatenating strings, shorthand versions, formatting strings, counting the length, checking if a string has a substring in it, and more. To get started, let’s spin up a new test website. I’ll walk you through the process using: DesktopServer PhpStorm UpDevTools Kint PHP Debugger plugin Getting a Kint Error? If you are getting a d() or ddd() function not found fatal error with […]