?
Solved

str_replace Question

Posted on 2006-04-11
5
Medium Priority
?
265 Views
Last Modified: 2008-03-06
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.
0
Comment
Question by:DReade83
  • 3
  • 2
5 Comments
 
LVL 14

Accepted Solution

by:
CtrlAltDl earned 2000 total points
ID: 16432064
How about this:

if (strlen($text)>0) {
     $text = str_replace("", "No Information", $text);
} else {
     $text = "No Information";
}
0
 
LVL 14

Expert Comment

by:CtrlAltDl
ID: 16432080
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";
}
0
 

Author Comment

by:DReade83
ID: 16432098
Thank you very much!!! Works a treat! :-D
0
 

Author Comment

by:DReade83
ID: 16432106
Sorry, posted too soon. The first answer worked no probs. Thanks again.
0
 
LVL 14

Expert Comment

by:CtrlAltDl
ID: 16432185
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.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses
Course of the Month17 days, 2 hours left to enroll

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question