Skip to main content

String Functions

String functions help you format, validate, and transform text content. Instead of displaying raw data that might be too long, improperly formatted, or unsafe, these functions let you clean up text, control length, and ensure content displays exactly as intended.

Why String Functions Matter

Raw text challenges:

  • Post titles that are too long for card layouts
  • Misbehaving HTML tags in post content: "<p>Welcome to our site</p>"
  • Inconsistent capitalization in user-generated content
  • Dates showing as timestamps: 1699833600 instead of "November 12, 2023"

What string functions solve:

  • Length control: Trim long titles to fit design layouts
  • Safe display: Remove HTML tags and escape special characters
  • Consistent formatting: Standardize capitalization and styling
  • User-friendly dates: Convert timestamps to readable formats

Text Transformation

Change the appearance and format of your text content.

upper/lower

Convert to Uppercase / Convert to Lowercase.

upper(text)    # Convert to UPPERCASE
lower(text) # Convert to lowercase

escape

Essential for displaying user content safely by converting HTML tags to visible text.

escape(text)

When to use:

  • Display HTML code examples in tutorials
  • Show user-submitted content safely
  • Prevent HTML injection in comments

Text Length and Control

Manage text length to fit your design layouts and improve readability.

count

Check text length for validation, layout decisions, and content analysis.

count(text)

When to use:

  • Check if titles fit design layouts
  • Validate form input lengths
  • Show character counts to users

limit_characters

Control exact text length while preserving readability.

limit_characters(text, max_length, start_position)

When to use:

  • Fit text into fixed-width design elements
  • Create consistent card layouts
  • Show text previews with exact character limits

limit_words

Maintain readable text flow by cutting at word boundaries instead of mid-word.

limit_words(text, word_count, start_position)

When to use:

  • Create readable excerpts and previews
  • Maintain text flow in card layouts
  • Show consistent content lengths across posts

Text Search and Validation

Find specific content within text and validate text patterns.

includes

Essential for conditional content display and search functionality.

includes(search_for, search_in)

When to use:

  • Conditional content based on post titles or content
  • Search result highlighting
  • Category and tag filtering

match

Advanced text validation and pattern detection for complex text analysis.

match(pattern, text)

When to use:

  • Validate email formats and URLs
  • Extract specific text patterns
  • Complex content filtering

Number and Currency Formatting

Transform numbers into professional, readable formats.

number_format

Make large numbers readable and format currencies consistently.

number_format(number, decimals, decimal_point, thousands_separator)

When to use:

  • Display prices and financial data
  • Format statistics and counts
  • International number formatting

sprintf

Create exactly formatted strings with dynamic values for consistent output.

sprintf(format_string, value1, value2, ...)

Format codes:

  • %s - String
  • %d - Integer
  • %.2f - Float with 2 decimals
  • %% - Literal % symbol

When to use:

  • Consistent currency formatting
  • Template-based text generation
  • Multi-language number formatting

Date and Time Handling

Convert timestamps to human-readable dates and parse date strings.

date

Transform WordPress timestamps into any date format you need.

date(format, timestamp)

Common formats:

  • 'F j, Y' - November 12, 2023
  • 'M d, Y' - Nov 12, 2023
  • 'Y-m-d' - 2023-11-12
  • 'g:i A' - 2:30 PM

When to use:

  • Display publication dates
  • Format event dates and times
  • Create consistent date styles

strtotime

Convert date strings into timestamps for calculations and comparisons.

strtotime(date_string, base_timestamp)

When to use:

  • Parse user-entered dates
  • Calculate relative dates
  • Convert various date formats to timestamps

Type Conversion

Convert between different data types for calculations and formatting.

intval/floatval

Convert to Whole Number / Convert to Decimal

intval(value)      # Convert to integer
floatval(value) # Convert to float/decimal

When to use:

  • Clean user input for calculations
  • Convert custom field values to numbers
  • Ensure proper data types for math operations