0

I have a string in php that can contain anything, depending on what the user puts on. Let's assume I have this string

$str = 'I am coming tomorrow<span class="_21wk" style="background-url:(&quot;/images/laugh.gif&quot;)"> </span>';
$strHtml = htmlentities($str);

Now if I do this

echo '<a onclick="$('div').html(\''.$strHtml.'\')">click</a>';

I will be having a symbol like a question mark in a trapezium shape in the space within the span tag. Please how do I fix this?

1 Answers1

-1

You can create a hidden div element using CSS display property. Special character is appeared because of htmlentities api.

string htmlentities ( string $string [, int $flags = ENT_COMPAT | ENT_HTML401 [, string $encoding = ini_get("default_charset") [, bool $double_encode = true ]]] )

encoding:

An optional argument defining the encoding used when converting characters.

If omitted, the default value of the encoding varies depending on the PHP version in use. In PHP 5.6 and later, the default_charset configuration option is used as the default value. PHP 5.4 and 5.5 will use UTF-8 as the default. Earlier versions of PHP use ISO-8859-1.

Although this argument is technically optional, you are highly encouraged to specify the correct value for your code if you are using PHP 5.5 or earlier, or if your default_charset configuration option may be set incorrectly for the given input.

<?php
    $str = '<div class="hidden-laugh" style="display:none">I am coming tomorrow<span class="_21wk" style="background-url:(\'/images/laugh.gif\')"> </span></div>';

    echo $str;
    echo '<a id="hidden-laugh" href="#">click</a>';
?>

Include the jquery library if it's not done before

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>

You can display the content based on user click

<script type="text/javascript">
    $(document).ready(function(e) {
        $('#hidden-laugh').on('click', function(e) {
            alert('here');
            $(".hidden-laugh").removeAttr('style');         
        });
    });
</script>
Sundar
  • 4,580
  • 6
  • 35
  • 61
  • 1
    This reads more like a game of spot-the-difference. What did you change? Why should that solve the problem? – Quentin May 22 '17 at 12:51
  • i have already informed him about the change in his comments about the `quote` before pressing down votes please check the comments. User is very clear about his mistake – Sundar May 23 '17 at 09:25
  • Answers should be answers. They shouldn't be half of an answer with the rest of it buried in the comments. The use of `"` to put a `"` around the URL is absolutely fine. Changing it to a `'` won't make a difference. – Quentin May 23 '17 at 09:26
  • This still makes absolutely no difference. It doesn't fix the problem. – Quentin May 23 '17 at 09:37
  • The OP replied to your original comment saying that it make no difference. When you checked you, presumably, were simply unable to reproduce the original problem and your trivial change wasn't relevant to it. – Quentin May 23 '17 at 10:14
  • `onclick` he is trying jquery call that'll never works until document ready. He have to use the approach which I updated on the answer. – Sundar May 23 '17 at 10:17
  • Nonsense. The ready event is useful to delay event binding until the element exists. The use of an onclick attribute (as in the question) won't attempt binding until the element exists, it's tied too closely to the creation of the element. What's more, the problem described in the question is that the invalid character symbol is displayed, not that the JavaScript function fails to be called. – Quentin May 23 '17 at 10:20
  • idiot. the special character issue will happen on the `html entities`. I updated alternative way in this case he'll never get the special character issue too. All characters updated in the issue is valid characters. Html entities removed http://php.net/manual/en/function.htmlentities.php check the encoding – Sundar May 23 '17 at 10:25
  • HTML entities will never cause the invalid character symbol to appear. The problem is with the value of `$strHtml` – Quentin May 23 '17 at 10:30
  • `An optional argument defining the encoding used when converting characters.` `If omitted, the default value of the encoding varies depending on the PHP version in use. In PHP 5.6 and later, the default_charset configuration option is used as the default value. PHP 5.4 and 5.5 will use UTF-8 as the default. Earlier versions of PHP use ISO-8859-1.` http://php.net/manual/en/function.htmlentities.php – Sundar May 23 '17 at 10:30
  • `$strHtml = htmlentities($str);` check this line in the question and answer. When you create `HIDDEN ELEMENT` no need any encoding basic sense – Sundar May 23 '17 at 10:33
  • My understanding of the `htmlentities` function is that it takes in a string encoded using *some encoding* and spits out ASCII (having replacing all the non-ASCII characters, along with `"`, `>`, etc) with entities. What input to it would result it in also generating a non-ASCII character that causes the invalid character to be shown? – Quentin May 23 '17 at 10:35
  • HTML entities will convert the characters to the configured encoding format too. The default setting may be UTF-8 `string htmlentities ( string $string [, int $flags = ENT_COMPAT | ENT_HTML401 [, string $encoding = ini_get("default_charset") [, bool $double_encode = true ]]] )` – Sundar May 23 '17 at 10:37
  • Convert what characters? I'd really like to see an example. (e.g. é in ISO-8850-15). – Quentin May 23 '17 at 10:38
  • in your example case the deafult configuration of your encoding. If you configure as UTF-8 in php.ini this will be converted to UTF-8. you can check the syntax of htmlentities function `string $encoding = ini_get("default_charset")` – Sundar May 23 '17 at 10:41
  • I don't have an example case. – Quentin May 23 '17 at 10:41
  • just check the encoding section of php document you will get more clarification http://php.net/manual/en/function.htmlentities.php – Sundar May 23 '17 at 10:44