1

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] "

2
  • Try and trim $meteo before 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]) Commented Apr 30, 2021 at 18:15
  • I would also get used to passing values into the functions rather than using global Commented Apr 30, 2021 at 18:16

2 Answers 2

1

You need to trim before you concatenate the quotes.

$new_meteo1 = '"' . rtrim($meteo) . '"';
$new_meteo2 = '"' . rtrim($meteo) . 'test';
Sign up to request clarification or add additional context in comments.

Comments

0

The source of the space is in $nomFichier[1]; therefore, you should rtrim that.

$meteo = "http://www.redacted/" . rtrim($nomFichier[1]);

For debugging in future you should examine the value of your data variable before you do any processing on it, because you can rule out your processing as the issue. Also consider using var_dump() instead of outputting the type, because this will show you the value enclosed in quotes, meaning you can spot any unexpected whitespace.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.