Juan.Queiroz
Juan.Queiroz

Reputation: 227

Same string with different length

I don't understand why the same string has different length. I'm talking about the output 1 and 4. Something wrong with encoding? The output 4, the string is scraped from one website.

Code:

public static function findCategoryByName($category){
    $value = "IT & Software";
    $category_1 = urlencode($value);
    $category_2 = urlencode('IT & Software');
    echo '<pre>';
    var_dump("1: " . $value);
    echo '</pre>';        
    echo '<pre>';
    var_dump("2: " . $category_1);
    echo '</pre>';
    echo '<pre>';
    var_dump("3: " . $category_2);
    echo '</pre>';
    echo '<pre>';
    var_dump("4: " . utf8_encode($category));
    echo '</pre>';
    echo '<pre>';
    var_dump("5: " . $category);
    echo '</pre>';
    return (new self)->find("name=:name", "name={$category}")->fetch();
}

Output:

string(16) "1: IT & Software"
string(18) "2: IT+%26+Software"
string(18) "3: IT+%26+Software"
string(20) "4: IT & Software"
string(24) "5: IT+%26amp%3B+Software"

Upvotes: 0

Views: 246

Answers (1)

Mihail0v
Mihail0v

Reputation: 103

Based on your original post, the $category value was IT &amp; Software which results that outcome:

string(16) "1: IT & Software"
string(18) "2: IT+%26+Software"
string(18) "3: IT+%26+Software"
string(20) "4: IT &amp; Software"
string(24) "5: IT+%26amp%3B+Software"

It seems that you execute it in browser, try to test your code in CLI and you will see the difference.

Upvotes: 1

Related Questions