i have a really strange problem where i spent many hours and without any success... . I have a contenteditable area on my website where users can select emoticons which one they can see instantly in their written text (in case of the contenteditable area). So for messages from user to user i do not care about the length of the text but for writing comments i do! I need to count all characters of the string.
Now i have the problem that emoticons are transmitted like that:
<img src="data:image/gif;base64,R0lGODlhAQABAAAAACwAAAAAAQABAAA=" class="emoticon emoticon-class-name-for-example-happy">
Okay for sure i want to count only 1 character for each emoticon so i wrote a regex and tried to replace all emoticons with a '1'. Afterwards i thought it is pretty easy with just strlen i get the number of used characters. But this works only in theory, but damn why... .
So my regex is:
<img[ ]src=["'].+?["'][ ]class=["']emoticon[ ].+?["'][>]
the next point was that i started to test my regex with the help of phpliveregex.com . The result you can see here. Just click on the preg_replace tab.
Now i was pretty sure that this has to work for me and i tried it. I wrote a function in PHP:
private function countCharactersOfSpecialUserInput($userInput) {
$wholeCharacters = 0;
$input_lines = 'This is a test
for<img src="data:image/gif;base64,R0lGODlhAQABAAAAACwAAAAAAQABAAA=" class="emoticon Girl">my
<img src="data:image/gif;base64,R0lGODlhAQABAAAAACwAAAAAAQABAAA=" class="emoticon Girl">regex
which<img src="data:image/gif;base64,R0lGODlhAQABAAAAACwAAAAAAQABAAA=" class="emoticon Girl">should
be alright <img src="data:image/gif;base64,R0lGODlhAQABAAAAACwAAAAAAQABAAA=" class="emoticon Not-Talking">and<img src="data:image/gif;base64,R0lGODlhAQABAAAAACwAAAAAAQABAAA=" class="emoticon Not-Talking">
match all this emoticons except things like <img dsopjfdojp
<img oew> because this ones are not real emoticons! The following is a real one: <img src="data:image/gif;base64,R0lGODlhAQABAAAAACwAAAAAAQABAAA=" class="emoticon Girl">
';
return preg_replace("/<img[ ]src=[\"'].+?[\"'][ ]class=[\"']emoticon[ ].+?[\"'][>]/", "1", $input_lines);
}
In my function i does not count the characters right now because there is a bug, which i do not understand. It will sound impossible but it is real :-(.
If i use the string which is safed in the variable $input_lines it works well. But if i use the text which a user can transmit it does not work!
I used var_dump as well as print_r to get the transmitted data from the user. Afterwards i used exactly this string and saved it in the input_lines variable. And the unbelievable fact is by using the input_lines variable it works again... . Doesn't matter what i do my code does not replace a single emoticon while the text was transmitted dynamically by the user... .
Is there anything where you could imagine what could case this problem? I am clueless and i can not believe that this is real. It has to work i tried so many other things about that but nothing worked for me... .
strlening the original source data (containing the emoticon's code), in stead of rendered data (containing the img elements)?DOMDocument.