Advanced PHP Strings

Working with multibyte strings, Unicode, and UTF-8 can feel intimidating when writing PHP. Advanced PHP Strings: Text analysis, generation, and parsing via. Laravel offers a guided experience navigating the different techniques to properly work with and manage multibyte strings. The book also offers extensive discussions around the Laravel string helper methods to provide a familiar reference point while navigating the intricacies of string and text manipulation.

Through hundreds of hand-crafted examples and in-depth discussions, you will learn how to:

  • Read and write multibyte strings using PHP
  • Safely loop through the characters of a multibyte string
  • Work with over sixty Laravel string helper methods and fluent strings
  • Manage documents with a variety of different line-ending styles
  • Convert string positions to line and column numbers (and back)
  • And so much more
Advanced PHP Strings

Release Date

2022-10-14

Page Count

749

Formats

PDF, EPUB, Print

Table of Contents

  • PHP Version
  • Laravel Version
  • Symbols Used In This Book
  • 1 What are Strings
    • 1.1 A Brief History of Character Encodings
    • 1.2 Unicode and UTF-8
    • 1.3 Implementing Uppercase and Lowercase Functions
    • 1.4 Iterating Multibyte Strings
    • 1.5 Accessing Individual Characters in a UTF-8 String
    • 1.6 Managing Character Encodings
    • 1.7 Types of PHP Strings
    • 1.8 Backticks and Template Strings
    • 1.9 The Laravel String Helpers
  • 2 Fluent Strings
    • 2.1 Converting Fluent String Objects to Strings
    • 2.2 Conditional String Logic
  • 3 The Formatting Helper Methods
    • 3.1 Formatting Strings with camelCasing
    • 3.2 Formatting Headlines
    • 3.3 Formatting Strings with "kebab-casing"
    • 3.4 Lowercasing the First Character of a String
    • 3.5 Creating Lowercase Variants of a String
    • 3.6 Reversing the Characters of a String
    • 3.7 Creating a URL from a Title
    • 3.8 Formatting Strings with snake_casing
    • 3.9 Removing Extra Whitespace from a String
    • 3.10 Formatting Strings with StudlyCasing
    • 3.11 Formatting Strings with Title Casing
    • 3.12 Uppercasing the First Character of a String
    • 3.13 Generating Upper Cased Variants of Strings
    • 3.14 Counting the Number of Words in a String
  • 4 The Logical Helper Methods
    • 4.1 Checking If a String Contains Another String
    • 4.2 Checking If a String Contains Multiple Substrings
    • 4.3 Checking If a String Contains Another String
    • 4.4 Checking If a String Starts with Another String
    • 4.5 Checking If a String Contains Only ASCII Characters
  • 5 The Construction Helper Methods
    • 5.1 Transliterating Characters to ASCII
      • Language Codes
    • 5.2 Generating Excerpts of Text
    • 5.3 Ensuring a String Always Ends with a Given String
    • 5.4 Calculating String Length
    • 5.5 Truncating Strings
    • 5.6 Obscuring Parts of a String
      • You Probably Shouldn't Store Credit Card Numbers
    • 5.7 Generating Pseudo-Random Strings
    • 5.8 Removing Substrings or Characters from Strings
    • 5.9 Repeating Strings and Characters
    • 5.10 Replacing Substrings
    • 5.11 Sequentially Replacing Substrings in a String
    • 5.12 Replacing the First Occurrence of a Substring or Character
    • 5.13 Replacing the Last Occurrence of a Substring or Character
    • 5.14 Ensuring Strings Always Begin with a Specific Prefix
    • 5.15 Replacing a Key/Value Pair of Strings within a String
    • 5.16 Splitting Strings on Uppercase Characters
  • 6 The Extraction Helper Methods
    • 6.1 Returning Part of a String after Another String or Character
    • 6.2 Returning a Substring after the Last Occurrence of a String or Character
    • 6.3 Returning Part of a String before Another String or Character
    • 6.4 Retrieving All Content before the Last Occurrence of a Substring or Character
    • 6.5 Retrieving Content between Two Search Strings
    • 6.6 Retrieving the Smallest Amount of Content between Two Search Strings
    • 6.7 Limiting the Number of Words in a String
    • 6.8 Getting Portions of a String
    • 6.9 Counting Occurrences of a Search String
    • 6.10 Replacing Strings within a Portion of an Existing String
  • 7 Padding Strings
    • 7.1 Padding the Beginning of Strings
    • 7.2 Padding the End of Strings
    • 7.3 Padding Both Sides of a String
  • 8 String Translations; Singular and Plural Word Forms
    • 8.1 Working with Variable Replacements
    • 8.2 Managing Ranges of Items
    • 8.3 Singular and Plural Word Forms
  • 9 Extending Strings with Macros and Mixins
  • 10 Lines and Words
    • 10.1 Positions, Lines, and Columns
    • 10.2 Splitting Strings into Words
    • 10.3 Counting Word Occurrences
    • 10.4 Finding Frequent and Repeated Words
    • 10.5 Stop Words
    • 10.6 Constructing Initialisms and Acronyms
    • 10.7 Calculating Estimated Reading Time
  • 11 More Lines and Words
    • 11.1 Locating Words at Arbitrary Positions
    • 11.2 Locating Neighboring Words and Characters
    • 11.3 Retrieving Adjacent Lines
    • 11.4 Managing Text Indentation
    • 11.5 Implementing an IndentationManager Class
  • 12 Applied Techniques: Writing a Gherkin Parser
    • 12.1 Modeling Gherkin
    • 12.2 A Gherkin Lexer
    • 12.3 Implementing Our Gherkin Parser
      • Parsing Gherkin Comments
      • Parsing Names
      • Parsing Step Text
      • Parsing Free-Form Gherkin Descriptions
      • Parsing Scenario Tags
      • Parsing Step Parameters
      • Parsing Doc Strings
      • Parsing Gherkin Data Tables
      • Parsing Scenario Examples
      • Parsing Steps
      • Parsing a Single Scenario and Background
      • Parsing Scenario Outlines and Templates
      • Parsing a Single Rule and Multiple Rules
      • Parsing Features
    • 12.4 Adding Localization Support to the Gherkin Parser
    • 12.5 Other Applications of Indentation-Based Parsers
  • 13 Markov Chains and Text Generation
    • 13.1 Writing a Markov Text Generator
    • 13.2 Weighted Randomized Selection
    • 13.3 Improving our Text Generator
  • 14 Fixed Width Data Parsing
    • 14.1 Managing Data Conversions
    • 14.2 Updating an Individual Record's Values
    • 14.3 Techniques for Managing Text Truncation
  • 15 Splitting Strings
    • 15.1 Basic String Splitting
    • 15.2 Splitting Strings Using Regular Expressions
    • 15.3 Extracting YAML Frontmatter from Documents
    • 15.4 Parsing Single-File Components
    • 15.5 Splitting Input Containing Substrings with the Delimiter
    • 15.6 Implementing a String Cursor System
    • 15.7 Implementing the StringsCursor
    • 15.8 Implementing the ClosureStringCursor
    • 15.9 A Simple PHP Tokenizer
    • 15.10 Working With SQL-Style Strings
    • 15.11 Splitting Strings Containing Paired Brackets
  • 16 Applied Techniques: A Blade Directive Validator
    • 16.1 Scaffolding our Parser Implementation
    • 16.2 Parsing Blade Directives
    • 16.3 Parsing Directive Parameters
    • 16.4 Validating Blade Directives
    • 16.5 Ensuring Consistent Directive Casing
    • 16.6 Enforcing Spacing After Directive Names
  • 17 Working with HTML
    • 17.1 Reversing Double Encoded HTML Entities
    • 17.2 Working with Embedded Languages
    • 17.3 Parsing Fragmented HTML Documents
    • 17.4 Parsing HTML Attributes and Parameters
    • 17.5 Analyzing Fragmented HTML Documents
  • 18 Regular Expressions
    • 18.1 Regular Expression Syntax Primer
      • Meta-Characters
      • Anchors
      • Escape Sequences
      • Character Classes
      • Sub-Patterns, Capture Groups, and Non-Capturing Groups
      • Repeating Characters and Patterns
      • Ensuring Matches Do Not Start or End with a Specific Pattern
  • Appendix A: UTF-8 String Iterator Implementation
  • Appendix B: CharacterPositionLocator Implementation
  • Appendix C: IndentationManager Implementation
  • Appendix D: Gherkin Parser Implementation
  • Appendix E: Markov Chain Implementation
  • Appendix F: Single-File Component Parser Implementation
  • Appendix G: String Cursor Implementation
  • Appendix H: String Cursors
  • Appendix I: Blade Directive Validator Implementation
  • Appendix J: HTML Fragments Parser and Analyzer Implementation
Stillat

Covering topics from JavaScript to Laravel, and everything in-between since 2012.

Resources

© Stillat, LLC. All right reserved.
Code highlighting provided by Torchlight