1

I am using Joomla as a CMS for my website, and I am having some problems making my website fully W3C compliant when embedding a map on my page. The module I am using to embed a map uses this piece of code to embed the map.

<iframe height="<?php echo $module_height; ?>" 
         style="border:<?php echo $border; ?>; 
         width:<?php echo $module_width.$module_width_unit; ?>" 
         src="http://maps.google.com/maps?q=<?php 
for ($loop = 0; $loop < $keywords_number; $loop += 1) {
    echo $keywords[$loop];
    if($loop!=$keywords_number_1) {
        echo "+";
        }
}

?>&amp;ie=UTF8&amp;view=map&amp;f=q&amp;saddr=<?php

for ($loop = 0; $loop < $keywords_number; $loop += 1) {
    echo $keywords[$loop];
    if($loop!=$keywords_number_1) {
        echo ",+";
    }
}

?>&amp;<?php if($satellite) { ?>t=h&amp;<?php } ?>output=embed"></iframe>

Unfortunately, this results in a return like this,

<iframe height="300" style="border:none; width:100%" src="http://maps.google.com/maps?q=STREET ADDRESS
CITY,+PROVINCE+POSTAL+CODE
&amp;ie=UTF8&amp;view=map&amp;f=q&amp;saddr=STREET ADDRESS
CITY,,+PROVINCE,+POSTAL,+CODE
&amp;output=embed"></iframe>

The maps show up perfectly fine in the website, but I get this error,

Bad value for attribute src on element iframe: Tab, new line or carriage return found.

I would like this website to be fully W3C compliant, and I assume that I need all of the text in the output to be on one line.

Is there any way that I can force the for loops to print on the same line, or concatenate the output of the two loops into a string to stay on the same line?

Thanks!

Mike
  • 22,114
  • 13
  • 72
  • 84
ArzaanK
  • 185
  • 2
  • 7
  • First of all, why not build the entire parameter string in PHP before even beginning to output anything to HTML. That will at least clean up and help prevent spurious whitespace due to the spaghetti code nature in which the code is currently written. – Mike Brant May 24 '16 at 19:44
  • 1
    Stop using complicated loops to do `join`'s job. – CBroe May 24 '16 at 19:57
  • The way your `php` tags are placed, there is no obvious reason why you should get those newlines. Probably part of some of the values you are outputting in those loops? Then you should `trim` excess whitespace first. (No need to revert back to a loop for that, `array_map` helps.) – CBroe May 24 '16 at 20:01
  • Possible duplicate of [Remove new lines from string](http://stackoverflow.com/questions/3760816/remove-new-lines-from-string) – Eaten by a Grue May 25 '16 at 14:07

2 Answers2

1

Try by replacing all the whitespace characters in the vars that you put in the src url with a simple space:

<iframe height="<?php echo $module_height; ?>" style="border:<?php echo $border; ?>; width:<?php echo $module_width.$module_width_unit; ?>" src="http://maps.google.com/maps?q=<?php                                                                                                                                                                                                        
for ($loop = 0; $loop < $keywords_number; $loop += 1) {
    echo $keywords[$loop];
    if($loop!=$keywords_number_1) {
        echo "+";
    }
}

?>&amp;ie=UTF8&amp;view=map&amp;f=q&amp;saddr=<?php

for ($loop = 0; $loop < $keywords_number; $loop += 1) {
    echo trim(preg_replace('/\s+/', ' ', $keywords[$loop]));
    if($loop!=$keywords_number_1) {
        echo ",+";
    }
}

?>&amp;<?php if($satellite) { ?>t=h&amp;<?php } ?>output=embed"></iframe>
Tudor Constantin
  • 25,256
  • 7
  • 48
  • 70
1

A few suggestions:

  • Stop writing this spaghetti code
  • Consider replacing loops with implode() calls.
  • Consider building your query parameter as data structure and then converting to query string in one pass.
  • Properly URL-encode the parameter string

I might write this code like:

<?php
$google_map_base_url = 'http://maps.google.com/maps?';

// prepare parameters
$parameters = array();
$parameters['q'] = implode('+', $keywords);
$parameters['ie'] = 'UTF8';
$parameters['view'] = 'map';
$parameters['f'] = 'q';
$parameters['saddr'] = implode(',+', $keywords);
if($satellite) {
    $parameters['t'] = 'h';
}
$parameters['output'] = 'embed';

// build url-encoded query string
$query_string = http_build_query($parameters, '', '&amp;');

// build final url
$google_map_url = $google_map_base_url . $query_string;

// now output to browser
?>
<iframe height="<?php echo $module_height; ?>" style="border:<?php echo $border; ?>; width:<?php echo $module_width.$module_width_unit; ?>" src="<?php echo $google_map_url; ?>"></iframe>
Mike Brant
  • 68,891
  • 9
  • 93
  • 99