I am trying to create a function in php with the purpose of outputting a URL wrapped in quotation marks " ".
I am concatenating an existing URL with a variable but php is adding an extra space at the end of the first string before concatenating.
Troubleshooting:
I already tried str_replace() and rtrim() but no success.
I tried the instructions at Why does fgets adds a space at the end of string? and it does generate the last blank space like so
[0a]
[22] "
I am still unable to resolve it with the functions proposed. The final goal is to get the URL without spaces between .../data.txt and "
Here is the function with added troubleshooting code:
<?php
function meteoURL () {
global $nomFichier;
$meteo = "http://www.redacted/" . $nomFichier[1];
echo gettype($meteo); //string
?> <br> <?php
echo gettype($nomFichier[1]); //string
?> <br> <?php
$new_meteo1 = '"' . $meteo . '"';
$new_meteo2 = '"' . $meteo . 'test';
$fichierMeteo1 = str_replace(' ', '', ($new_meteo1));
$fichierMeteo2 = rtrim($new_meteo2);
?> <br> <?php
echo rtrim($new_meteo1);
//outputs with empty space between data.txt and "
//"http://www.iro.umontreal.ca/~dift1147/pages/TPS/tp1/data.txt "
?> <br> <?php
echo rtrim($new_meteo2);
//outputs with empty space between data.txt and test
//"http://www.iro.umontreal.ca/~dift1147/pages/TPS/tp1/data.txt test
?> <br> <?php
echo "after rtrim(): " . $fichierMeteo2;
?> <br> <?php
//this part is for troubleshooting only
foreach(str_split($fichierMeteo1) as $chr) {
printf("[%02x] %s <br />",ord($chr),$chr);
}
}
and here is the browser output:
string
string
"http://redacted/data.txt "
"http://redacted/data.txt test
after rtrim(): "http://redacted/data.txt test
[22] "
[68] h
[74] t
[74] t
[70] p
[3a] :
[2f] /
[2f] /
... redacted
[2e] .
[74] t
[78] x
[74] t
[0a]
[22] "
$meteobefore you add the quotes. After you've added them,rtrim()won't work since that trims away white spaces in the end of the string, which then is"and not white space. Or even trim ` $nomFichier[1]` when concatenating it in the beginning:"http://www.redacted/" . trim($nomFichier[1])global