I am trying to remove the spaces from $contactname. Right now if I view the code below I get:
First Last
If I replace $_SESSION[‘name’] with ‘first last’ I get:
firstlast
Any ideas on why this is only working when it is a static field?
$contactname=$_SESSION['name'];
$contactname = preg_replace('/( *)/', '', $contactname);
echo $contactname."\n";
Updated Code with same problem:
$contactname=$_SESSION['name'];
$contactname = str_replace(' ', '', $contactname);
echo $contactname."\n";
Your regex should use the character class for whitespace,
\s:In order to replace anything like a space that might be in $_SESSION, or use
str_replaceif it is guaranteed to only be spaces (because it doesn’t need the regex engine and goes faster):