Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When selecting all, there is always a space at the end

Tags:

html

browser

php

I have wrote this little script which generates a nice random password. The problem I am having is that when you open the page and select all + copy the text into the clipboard. There is always a space at the end even if I use the trim. Is there anyway to fix this?

Tested using Firefox.

UPDATE: Ok after testing with a jsfiddle example this has nothing to do with PHP because I get the same issue here. This has the same problem of always adding a space at the end.

    <?php
function random_readable_pwd($length=10){

    // the wordlist from which the password gets generated 
    // (change them as you like)
    $words = 'AbbyMallard,AbigailGabble,AbisMal,Abu,Adella,TheAgent,AgentWendyPleakley,Zini';

    // Split by ",":
    $words = explode(',', $words);
    if (count($words) == 0){ die('Wordlist is empty!'); }

    // Add words while password is smaller than the given length
    $pwd = '';
    while (strlen($pwd) < $length){
        $r = mt_rand(0, count($words)-1);
        $pwd .= $words[$r];
    }

    $num = mt_rand(1, 99);
     if ($length > 2){
        $pwd = substr($pwd,0,$length-strlen($num)).$num;
    } else { 
        $pwd = substr($pwd, 0, $length);
    }

   $pass_length = strlen($pwd);
   $random_position = rand(0,$pass_length);

   $syms = "!@#%^*()-?";
   $int = rand(0,9);
   $rand_char = $syms[$int];

   $pwd = substr_replace($pwd, $rand_char, $random_position, 0);

    return $pwd;
}
?>
<html><head><title>Password generator</title></head>
<body><?php echo random_readable_pwd(10); ?></body>
</html>
like image 639
John Magnolia Avatar asked Dec 06 '25 12:12

John Magnolia


1 Answers

It doesn't matter what do you server-side. In the end, you get this:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head><title></title>
</head>
<body>foo</body>
</html>

(I've added a DOCTYPE to avoid ambiguities.) If you inspect the HTML as interpreted by Firefox (you'll need Firebug for that) you'll find this:

<body>foo </body>

So need to generate a different HTML markup. One possibility:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head><title></title>
</head>
<body>
    <p>foo</p>
</body>
</html>
like image 146
Álvaro González Avatar answered Dec 09 '25 01:12

Álvaro González



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!