LMR
LMR

Reputation: 23

Remove multiple commas in PHP

Sorry if this is really basic but I am just learning PHP.

I have some code that looks like:

<h4><?=$prop['house_number']?>, <?=$prop['street']?>, <?=$prop['town']?></h4>

That is bring back from the database eg: 55, main street, townname

When the address does not contain a street name it comes back as eg: 55, , townname.

I want to know how to remove the commer so it just brings back eg: 55, townname.

Hopefully it is a really easy one but I have tried a couple of things and cannot seem to get it right.

Many thanks

Upvotes: 2

Views: 728

Answers (7)

faino
faino

Reputation: 3224

You can use inline conditionals, try this:

<?php
    $house_num=($prop['house_number']) ? $prop['house_number'].',' : null;
    $street=($prop['street']) ? $prop['street'].',' : null;
    $town=($prop['town']) ? $prop['town'] : null;
    $h4=$house.' '.$street.' '.$town;
?>

Then in use:

<h4><?php echo($h4); ?></h4>

Upvotes: 2

shawndreck
shawndreck

Reputation: 2069

This just another way, you could save the address string in a variable and replace double commas[,,] with a single one.Similar to the idea that hakre is sugessting.

Example:

<?php $str = $prop['house_number']  . ',' . $prop['street'] . ',' . $prop['town']?> ;?>
<h4><?=str_replace(',,','',$str);?></h4>

Upvotes: 0

pp19dd
pp19dd

Reputation: 3633

Suggest you get used to array functions, and implode; assuming that your $prop array looks like this:

$prop = array(
    "house_number" => 55,
    "street" => "",
    "town" => "Houston"
);

You can filter the array and use implode:

function filter_empty( $a ) {
    if( strlen(trim($a)) > 0 ) return( true );
    return( false );
}

$filtered = array_filter( $prop, 'filter_empty' );

From thereon, the array will look like this:

print_r( $filtered );
/*
Array
(
    [house_number] => 55
    [town] => Houston
)
*/

The human-readable line of text is easy: echo implode( ", ", $filtered );

WIll output something like 55, Houston - bypassing all empties.

Upvotes: 1

DaveE
DaveE

Reputation: 1643

You could use ternary operators to check if each var is empty. Have a look at http://davidwalsh.name/php-shorthand-if-else-ternary-operators

The code would then look like :

<h4>
<?=!empty($prop['house_number']) ? $prop['house_number'].', ' : ''?>
<?=!empty($prop['street']) ? $prop['street'].', ' : ''?>
<?=!empty($prop['town']) ? $prop['town'].', ' : ''?>
</h4>

Upvotes: 1

hakre
hakre

Reputation: 198209

You can fix your output later on:

ob_start();
?>
<h4><?=$prop['house_number']?>, <?=$prop['street']?>, <?=$prop['town']?></h4>
<?php
echo strtr(ob_end_clean(), ', , ', ', ');

But actually there are hundred ways to solve it, this is only one.

echo '<h4>' , implode(', ', array_filter(
        array($prop['house_number'], $prop['street'], $prop['town']), 'strlen')
      ),  '</h4>';

Upvotes: 1

Ayush
Ayush

Reputation: 42450

Use an array to hold the values, and implode()

Something like this:

$result = array();

if (!empty($prop['house_number'])
    $result[] = $prop['house_number'];

if (!empty($prop['street'])
    $result[] = $prop['street'];

if (!empty($prop['town'])
    $result[] = $prop['town'];

$result = implode(',',$result);

Upvotes: 3

Nico Burns
Nico Burns

Reputation: 17109

Replace

<?=$prop['street']?>,

with

<?php if(strlen($prop['street'] > 0) echo $prop['street'] . ", "; ?>

The old code outputs the street name followed by a comma, the new code checks if the street name is longer than 0 characters, and only outputs if it is.

Upvotes: 2

Related Questions