remove extra spaces in word php code example

Example 1: php remove last character in string

//Remove the last character using substr
$string = substr($string, 0, -1);

Example 2: remove whitespace from string php

$str = "\n\n\nHello World!\n\n\n";
echo "With trim: " . trim($str);

Example 3: php insert hyphen into spaces in string

$test = "jjfnj 948";
$test = str_replace(" ", "", $test);  // strip all spaces from string
echo substr($test, 0, 3)."-".substr($test, 3);  // isolate first three chars, add hyphen, and concat all characters after the first three

Tags:

Php Example