Link to home
Start Free TrialLog in
Avatar of DReade83
DReade83Flag for United Kingdom of Great Britain and Northern Ireland

asked on

str_replace Question

I want to get the following line to work:

$text = str_replace("", "No Information", $text);

Basically, I want it to replace a blank answer with "No Information". Unfortunately when I insert this, it doesn't work. I've tried replacing the "" with NULL, 0 and "0", but neither work.

Can anyone help? Thanks in advance.
ASKER CERTIFIED SOLUTION
Avatar of CtrlAltDl
CtrlAltDl
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Or better yet you could trim any blank space and check if it is null too.

if (strlen(trim($text))>0 || !is_null($text)) {
     $text = str_replace("", "No Information", $text);
} else {
     $text = "No Information";
}
Avatar of DReade83

ASKER

Thank you very much!!! Works a treat! :-D
Sorry, posted too soon. The first answer worked no probs. Thanks again.
Or you could condense it to one line like this:

$text = (strlen(trim($text))>0 || !is_null($text)) ? str_replace("", "No Information", $text) : "No Information";

Thanks for the points.