1

The Content is generated dynamically based on the php conditions .

Sample Content With Multiple White Space ( View Source )

<title>Samsung   Phones Under Rs.20000 </title>

How Title is Generating:

$conditional_title = <?php echo $brand; ?><?php echo $os; ?> Phones <?php echo ($prRange !='') ? 'under Rs.'$price' : ''; ?>

<title><?php echo $conditional_title; ?></title>

So If $os condition Fails , the content is generated with multiple white spaces , How can i trim these multiple white spaces in between the content.

Expected Output :

<title>Samsung Phones Under Rs.20000 </title>
Raaga
  • 569
  • 1
  • 5
  • 17

4 Answers4

4
$conditional_title= preg_replace( '/\s+/', ' ', $conditional_title);

That should do the trick, trims all excess whitespace into a single space

Caweren
  • 236
  • 1
  • 9
0

You may use function trim(). http://php.net/manual/ru/function.trim.php

<?php
$brand = trim($brand);
$os = trim($os);

$addition = '';
if($prRange != '')
    $addition = 'under Rs.'.$price;

$conditional_title = $brand.' '.$os.' '.Phones.' '.$addition;
?>

<title><?php echo $conditional_title; ?></title>
fiction
  • 566
  • 1
  • 6
  • 21
0

You can check if a variable is not empty:

$conditional_title = (!empty($brand)?$brand:"").(!empty($os)?$os:"")." Phones ".($prRange !='' ? 'under Rs.'$price' : '');

$conditional_title= preg_replace( '/[ ]+/', ' ', $conditional_title);//removing extra space if there is still any.?>

<title><?php echo $conditional_title; ?></title>

Well I can not understand Why are you making multiple opening and closing of <?php ?> in

$conditional_title = <?php echo $brand; ?><?php echo $os; ?> Phones <?php echo ($prRange !='') ? 'under Rs.'$price' : ''; ?>
Shub
  • 2,686
  • 17
  • 26
0

try this :

$conditional_title = <?php echo (isset($brand))?$brand:''; ?><?php echo (isset($os))?$os:''; ?> Phones <?php echo ($prRange !='') ? 'under Rs.'$price' : ''; ?>

<title><?php echo $conditional_title; ?></title>
Youness
  • 1,468
  • 1
  • 9
  • 19