Oihana PHP

betweenSpaces.php

Table of Contents

Functions

betweenSpaces()  : string
Wraps an expression with spaces on both sides.

Functions

betweenSpaces()

Wraps an expression with spaces on both sides.

betweenSpaces([mixed $expression = '' ][, bool $useSpaces = true ][, string $separator = ' ' ][, bool $trim = true ]) : string

Calls between() with space characters as $left and $right. Handles strings, arrays, and optional suppression of spaces.

  • String: Simply prepends and appends a space.
  • Array: Concatenates array values using the specified $separator, then wraps the resulting string in spaces.
  • Other types: Converted to string before wrapping.
  • If $useSpaces is false, the expression is returned as-is without any surrounding spaces.
Parameters
$expression : mixed = ''

The value to wrap. Can be string, array, or any type convertible to string.

$useSpaces : bool = true

Whether to apply the surrounding spaces (default: true).

$separator : string = ' '

Separator to use when $expression is an array (default: ' ').

$trim : bool = true

Whether to trim existing $left/$right characters (default: true).

Tags
example
betweenSpaces('hello');                    // returns ' hello '
betweenSpaces('hello', false);             // returns 'hello'
betweenSpaces(['a', 'b', 'c']);            // returns ' a b c '
betweenSpaces(['a', 'b', 'c'], true, ','); // returns ' a,b,c '
betweenSpaces(123);                        // returns ' 123 '
author

Marc Alcaraz

since
1.0.6
Return values
string

The wrapped expression with spaces if $useSpaces is true; otherwise the original expression as string.


        
On this page

Search results