GWana
GWana

Reputation: 3

how to remove line break in php when data is blank

I am trying to figure out how to remove blank line breaks, and have searched this forum for answer. There are lots of them, but I'm not clear on where I need to put the code. This is my scenerio:

This is what comes up as results from search when the 'Address2' line is empty of data:

Office:
Boston
123 Stedman Street

Lowell, MA 01851
Phone Number:
555-453-1234
fax Number:
555-453-1236

This is the way it should be when 'Address2' is empty:

Office:
Boston
123 Stedman Street
Lowell, MA 01851
Phone Number:
555-453-1234
fax Number:
555-453-1236

How do I remove the line break and where does the code go?

Here is my code:

echo '<span class="productdescription"><p>Office:   </p></span></h2>';
echo $query_row['Office'].'<br>';
echo $query_row['Address1'].'<br>';
echo $query_row['Address2'].'<br>';
echo $query_row['City'].', ';
echo $query_row['State'].'  ';
echo $query_row['Zip'].'<br>';
echo '<p><strong>Phone Number: </strong></p>';
echo $query_row['Phone'].'<br>';
echo '<p><strong>Fax Number: </strong></p>';
echo $query_row['Fax'].'<br><br>';

Upvotes: 0

Views: 101

Answers (2)

makmonty
makmonty

Reputation: 465

If I understand it, you want everything in one line if there's only one address line, but a sepparate address block if there are two address lines.

What about this:

echo '<span class="productdescription"><p>Office:   </p></span></h2>';
echo $query_row['Office'].'<br>';
echo $query_row['Address1'];
if($query_row['Address2'] != "") echo '<br>'.$query_row['Address2'].'<br>';
echo $query_row['City'].', ';
echo $query_row['State'].'  ';
echo $query_row['Zip'].'<br>';
echo '<p><strong>Phone Number: </strong></p>';
echo $query_row['Phone'].'<br>';
echo '<p><strong>Fax Number: </strong></p>';
echo $query_row['Fax'].'<br><br>';

Upvotes: 0

aynber
aynber

Reputation: 23011

You'll want to check if there's any data before you echo it out:

echo $query_row['Address1'].'<br>';
if(!empty($query_row['Address2'])) // This will skip if the field if it's empty
    echo $query_row['Address2'].'<br>';
echo $query_row['City'].', ';

Upvotes: 3

Related Questions