Web Hosting Talk







View Full Version : Replacing Empty Value with N/A?... PHP


Ryan Barr
12-06-2004, 08:27 PM
Ok, I have this error on a open source thing I am coding. When setting up a new staff/admin user with the quick-form, it doesn't ask for name or e-mail. So when it is displayed in another table, it has name, username, and e-mail.

So the table is all distorted because name and e-mail are blank.

So I used this...

$er1 = $Show[First];
echo ereg_replace("", "N/A", $er1);
$er2 = $Show[Last];
echo ereg_replace("", "N/A", $er2);
$er3 = $Show[Email];
echo ereg_replace("", "N/A", $er3);

to replace the blank with N/A.... but then I get this error...

Warning: ereg_replace(): REG_EMPTY in /home/blizter/public_html/demo/shells/shell_salist.php on line 157

Warning: ereg_replace(): REG_EMPTY in /home/blizter/public_html/demo/shells/shell_salist.php on line 159

Warning: ereg_replace(): REG_EMPTY in /home/blizter/public_html/demo/shells/shell_salist.php on line 161

Warning: ereg_replace(): REG_EMPTY in /home/blizter/public_html/demo/shells/shell_salist.php on line 157

Warning: ereg_replace(): REG_EMPTY in /home/blizter/public_html/demo/shells/shell_salist.php on line 159

Warning: ereg_replace(): REG_EMPTY in /home/blizter/public_html/demo/shells/shell_salist.php on line 161

Warning: ereg_replace(): REG_EMPTY in /home/blizter/public_html/demo/shells/shell_salist.php on line 157

Warning: ereg_replace(): REG_EMPTY in /home/blizter/public_html/demo/shells/shell_salist.php on line 159

Warning: ereg_replace(): REG_EMPTY in /home/blizter/public_html/demo/shells/shell_salist.php on line 161

Warning: ereg_replace(): REG_EMPTY in /home/blizter/public_html/demo/shells/shell_salist.php on line 157

Warning: ereg_replace(): REG_EMPTY in /home/blizter/public_html/demo/shells/shell_salist.php on line 159

Warning: ereg_replace(): REG_EMPTY in /home/blizter/public_html/demo/shells/shell_salist.php on line 161

Warning: ereg_replace(): REG_EMPTY in /home/blizter/public_html/demo/shells/shell_salist.php on line 157

Warning: ereg_replace(): REG_EMPTY in /home/blizter/public_html/demo/shells/shell_salist.php on line 159

Warning: ereg_replace(): REG_EMPTY in /home/blizter/public_html/demo/shells/shell_salist.php on line 161

Warning: ereg_replace(): REG_EMPTY in /home/blizter/public_html/demo/shells/shell_salist.php on line 157

Warning: ereg_replace(): REG_EMPTY in /home/blizter/public_html/demo/shells/shell_salist.php on line 159

Warning: ereg_replace(): REG_EMPTY in /home/blizter/public_html/demo/shells/shell_salist.php on line 161

Warning: ereg_replace(): REG_EMPTY in /home/blizter/public_html/demo/shells/shell_salist.php on line 157

Warning: ereg_replace(): REG_EMPTY in /home/blizter/public_html/demo/shells/shell_salist.php on line 159

Warning: ereg_replace(): REG_EMPTY in /home/blizter/public_html/demo/shells/shell_salist.php on line 161

how do I replace a empty variable with N/A? hopefully using the ereg_replace or similar function?... thanks for any help

null
12-06-2004, 08:42 PM
// empty variable
$var = '';

// assign N/A value if empty
$var = $var ? $var : 'N/A';

// see the result
echo $var;


P.S. Don't use regexp for such simple task.

Ryan Barr
12-06-2004, 08:57 PM
Ok, tried your way, failed, it worked, but instead of just replacing the empty ones it replaced them all. So then I tried some if variable is blank, variable equals this instead, that failed.

So I conquered it. I went to the areas where they were displayed and did some if variable is blank echo N/A, else echo the variable. And that worked. So thanks for the help anyway =).

azizny
12-06-2004, 09:54 PM
Originally posted by Spookster187
Ok, I have this error on a open source thing I am coding. When setting up a new staff/admin user with the quick-form, it doesn't ask for name or e-mail. So when it is displayed in another table, it has name, username, and e-mail.

So the table is all distorted because name and e-mail are blank.

So I used this...

$er1 = $Show[First];
echo ereg_replace("", "N/A", $er1);
$er2 = $Show[Last];
echo ereg_replace("", "N/A", $er2);
$er3 = $Show[Email];
echo ereg_replace("", "N/A", $er3);

to replace the blank with N/A.... but then I get this error...

how do I replace a empty variable with N/A? hopefully using the ereg_replace or similar function?... thanks for any help

ereg_replace will need a third pramater..

use this instead:


<?
echo str_replace('','N/A',$string);
//It can also be an array for finding

$find[0] = ''
$find[1]= ' ';
$find[2] = '|';

echo str_replace($find,'N/a",$string);

//Which will reaplce all above values with nA
?>


Peace,

null
12-06-2004, 11:18 PM
Originally posted by Spookster187
Ok, tried your way, failed, it worked, but instead of just replacing the empty ones it replaced them all. So then I tried some if variable is blank, variable equals this instead, that failed.

So I conquered it. I went to the areas where they were displayed and did some if variable is blank echo N/A, else echo the variable. And that worked. So thanks for the help anyway =).

Sorry, my code worked only for a single variable, not for an array. Here is a working example:

str_replace('', 'N/A', $Show);

kneuf
12-07-2004, 05:33 PM
Just another suggestion, if all else fails:

<?
//loop through db records here...
if ($var = "" || !$var) {
$var = "N\A";
}
//end the loop

EXOWorks
12-07-2004, 11:34 PM
To replace all null entries in the array `Show` to n/a:



while(list($key,$val) = each($Show)) {
if($val == "") {
$Show[$key] = 'N/A';
}
}

null
12-07-2004, 11:37 PM
Or


foreach($Show as $key=>value)
$Show[$key] = $value ? $value : 'N/A';


:)

ThorN
12-09-2004, 03:30 PM
function defval($val, $def = 'n/a')
{
if ( $val && $val != '')
return $val;
else
return $def;
}


foreach($Show as $key=>value)
$Show[$key] = defval($value);


You should use quotes for your array keys, ex: $Show['Last']

Ryan Barr
12-11-2004, 04:07 PM
Thank you all for your suggestions! I will play around with all of them and see what works best for my next version, as I already released the other one =).

Thanks Again!