admin管理员组

文章数量:1122832

I am building an HTML element from a PHP array inside a foreach loop:

echo '<li><a href="#" onclick="return filterByCategory("'.ltrim($categoryID).'");">'.$categoryName.'</a></li>';

results in the HTML output:

<a href="#" onclick="return filterByCategory(" 514ac53a-a047-45eb-b89b-05f012fb2b5b");">Adults</a>

It always adds a space, even with ltrim (I've tried trim and htmlspecialcharacters too)

Within the same loop, var_dump outputs:

string(36) "514ac53a-a047-45eb-b89b-05f012fb2b5b"

so it feels like the input data is without a space. It is doing it to every value field in the array. I don't understand.

I am building an HTML element from a PHP array inside a foreach loop:

echo '<li><a href="#" onclick="return filterByCategory("'.ltrim($categoryID).'");">'.$categoryName.'</a></li>';

results in the HTML output:

<a href="#" onclick="return filterByCategory(" 514ac53a-a047-45eb-b89b-05f012fb2b5b");">Adults</a>

It always adds a space, even with ltrim (I've tried trim and htmlspecialcharacters too)

Within the same loop, var_dump outputs:

string(36) "514ac53a-a047-45eb-b89b-05f012fb2b5b"

so it feels like the input data is without a space. It is doing it to every value field in the array. I don't understand.

Share Improve this question asked Nov 22, 2024 at 16:43 andyg1andyg1 1,5433 gold badges14 silver badges21 bronze badges 4
  • I'm not able to replicate the described behavior. Can you confirm a minimal reproducible example which demonstrates the problem when independently tested? – David Commented Nov 22, 2024 at 16:51
  • I think it's because the browser doesn't know what to do with your string. The following closes the noclick attribute early onclick="return filterByCategory(" and spaces are required syntax between element attributes. – admcfajn Commented Nov 22, 2024 at 16:51
  • No need to concat with echo. Use a comma instead. – Markus Zeller Commented Nov 22, 2024 at 16:51
  • 2 Are you sure that's the HTML output? I think it's what you see in the Elements inspector, but not in View Source. – Barmar Commented Nov 22, 2024 at 17:10
Add a comment  | 

2 Answers 2

Reset to default 1

The problem is that you're using " to delimit both the onclick attribute and the string argument to the function. The result is that the double quote that begins the function argument is terminating the attribute, so the argument becomes a separate attribute.

Use single quotes for one of them.

echo '<li><a href="#" onclick="return filterByCategory(\''.ltrim($categoryID).'\');">'.$categoryName.'</a></li>';

I would check and see that your string really has what you think it has.

The "space" might be a non-printable character that is not identified as a proper space by ltrim but that displays as a space.

Try something like this and see if it does not solve your problem:

$$categoryID = preg_replace('/[^[:print:]]/', '', $$categoryID);

More details here: How to remove all non printable characters in a string?

本文标签: PHP echo function is adding whitespace at the start of a string without whitespaceStack Overflow