regex - PHP preg_replace Confusing error -


i have strange problem spent many hours , without success... . have contenteditable area on website users can select emoticons 1 can see instantly in written text (in case of contenteditable area). messages user user not care length of text writing comments do! need count characters of string.

now have problem emoticons transmitted that:

<img src="data:image/gif;base64,r0lgodlhaqabaaaaacwaaaaaaqabaaa=" class="emoticon emoticon-class-name-for-example-happy"> 

okay sure want count 1 character each emoticon wrote regex , tried replace emoticons '1'. afterwards thought pretty easy strlen number of used characters. works in theory, damn why... .

so regex is:

<img[ ]src=["'].+?["'][ ]class=["']emoticon[ ].+?["'][>] 

the next point started test regex of phpliveregex.com . result can see here. click on preg_replace tab.

now pretty sure has work me , tried it. wrote function in php:

private function countcharactersofspecialuserinput($userinput) {     $wholecharacters = 0;     $input_lines = 'this 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                     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 emoticons except things <img dsopjfdojp                     <img oew> because ones not real emoticons! following real one: <img src="data:image/gif;base64,r0lgodlhaqabaaaaacwaaaaaaqabaaa=" class="emoticon girl">                     ';           return preg_replace("/<img[ ]src=[\"'].+?[\"'][ ]class=[\"']emoticon[ ].+?[\"'][>]/", "1", $input_lines); } 

in function not count characters right because there bug, not understand. sound impossible real :-(.

if use string safed in variable $input_lines works well. if use text user can transmit not work!

i used var_dump print_r transmitted data user. afterwards used string , saved in input_lines variable. , unbelievable fact using input_lines variable works again... . doesn't matter code not replace single emoticon while text transmitted dynamically user... .

is there imagine case problem? clueless , can not believe real. has work tried many other things nothing worked me... .

the text images html snippet, therefore use dom parse it:

$input_lines = 'this 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 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 emoticons except things <img dsopjfdojp <img oew> because ones not real emoticons! following real one: <img src="data:image/gif;base64,r0lgodlhaqabaaaaacwaaaaaaqabaaa=" class="emoticon girl">';  $doc = new domdocument();  // suppress warnings @$doc->loadhtml($input_lines);  $imgs = $doc->getelementsbytagname("img"); $number_of_imgs = $imgs->length; echo "found $number_of_imgs images" . php_eol;  // plain text nodevalue of // whole snippet. $text = $imgs->item(0)->parentnode->nodevalue; $len = mb_strlen($text);  echo "text length: $len + $number_of_imgs(images)" . php_eol; 

see working: http://3v4l.org/mh5t6


Comments

Popular posts from this blog

node.js - Using Node without global install -

How to access a php class file from PHPFox framework into javascript code written in simple HTML file? -

java - Null response to php query in android, even though php works properly -