开发者

Whitespace problem

开发者 https://www.devze.com 2023-03-14 12:25 出处:网络
I am getting data from a database and which is used to populate the select element (list) on my page.

I am getting data from a database and which is used to populate the select element (list) on my page.

The data stored with which i开发者_Go百科t has two spaces in my DB: "ABC CDE".

When I put the value in the select list, it removes the other space, thus leaving it "ABC CDE". But every time I get the data and I log it, it still has two spaces.

The problem is that if I needlook up something in DB using the name does not return anything because of the difference in spaces.

$query = "SELECT * from Member.people";
 $logger->debug($query);
 $result = mysql_query($query);
 $num = mysql_num_rows($result);

 while ($row = mysql_fetch_array($result)) {
      $op = $row['Name'];                    //ABC  DE
      echo '<option>'.$op.'</option>';       //ABC DE

 }

can someone help?


Consecutive white spaces in HTML are collapsed:

In particular, user agents should collapse input white space sequences when producing output inter-word space.

They are preserved in attributes though. You have to set the value attribute correctly and it will work:

echo '<option value="' . $op . '">'.$op.'</option>';

If you don't do that the content of the elements is used as value:

This attribute specifies the initial value of the control. If this attribute is not set, the initial value is set to the contents of the OPTION element.

what, as you already noticed, will give you the value with only one space (as it is collapsed).


Maybe try to replace the space char to the escape string &nbsp;


multiple spaces in HTML get normalised in the view. That's why I can do something like this:

<p>Hello         World</p>

yet display as "Hello World".

Do a string replace of " " to &nbsp; - that should fix your problem:

$op = $row['Name'].replace(/\s/g,'&nbsp;');  
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号