Mudsaf 17 Posted August 17, 2019 Report Share Posted August 17, 2019 So i'm not still really familiar with regex and how does it work, but i have issue with my code. I have ¤ in my text and it gets replace with unicode ? block. https://en.wikipedia.org/wiki/Specials_(Unicode_block) for me and I don't get why. Here is the code I'm using. <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <style> #kool { width:800px; border:groove; margin:0 auto; padding:25px; } </style> </head> <body> <?php /* Test preg_replace function */ $preg_pattern = "/[^A-Za-z0-9!\"#%£&()=@\s]/"; if (isset($_GET['preg'])) { echo "<div id='kool'>"; echo "<b>Original string: </b> " . $_GET['preg']; echo "<hr>"; echo "<b>Preg pattern: </b> " . $preg_pattern; echo "<hr>"; echo "<b>Result: </b> " . preg_replace($preg_pattern,"",$_GET['preg']); echo "</div>"; } ?> <form method="get" action="preg_test.php"> <input type="text" name="preg" <?php if (isset($_GET['preg'])) { echo " value='" . $_GET['preg'] . "'"; } ?>> <input type="submit"> </form> </body> </html> And here is the string I'm trying to use. Test 0-9, Specialcharacters: !"#¤%&/()=? Result image below Any guides / help appreciated. Quote Link to post Share on other sites
Ingolme 1,034 Posted August 17, 2019 Report Share Posted August 17, 2019 Your code editor most likely has not encoded the file as UTF-8. Depending on which code editor you're using the way to do this is different. On Windows Notepad, there's an "encoding" dropdown in the same dialog which you should set to "UTF-8". Other text editors have encoding menus or an encoding option in the document properties. Quote Link to post Share on other sites
Mudsaf 17 Posted August 18, 2019 Author Report Share Posted August 18, 2019 (edited) Changed to UTF-8 (not sure if it were already), issue still persists somehow. The odd part is, this works when the £ is not added in the preg pattern. Edited August 18, 2019 by Mudsaf had wrong image Quote Link to post Share on other sites
dsonesuk 925 Posted August 18, 2019 Report Share Posted August 18, 2019 Yes but is url encoded from $_GET querystring might be %C2%A4 same as space would be %20 Quote Link to post Share on other sites
Mudsaf 17 Posted August 18, 2019 Author Report Share Posted August 18, 2019 (edited) Apparently my browser forces ¤ to be at url, even though i replace it with %C2%A4 (if this was what you meant). Rest of the stuff is encoded properly (chrome). But on edge it is encoded. (Image from Edge browser) Also tried urldecode the GET parameter. Edited August 18, 2019 by Mudsaf Quote Link to post Share on other sites
dsonesuk 925 Posted August 18, 2019 Report Share Posted August 18, 2019 What about forcing it encoded so there all read the same with php rawurlencode(). Quote Link to post Share on other sites
Mudsaf 17 Posted August 18, 2019 Author Report Share Posted August 18, 2019 How would that work with preg_replace()? Quote Link to post Share on other sites
dsonesuk 925 Posted August 18, 2019 Report Share Posted August 18, 2019 Probably not, but since "URLs can only be sent over the Internet using the ASCII character-set. Since URLs often contain characters outside the ASCII set, the URL has to be converted into a valid ASCII format." You ideally should be filtering a url that gives you the same result, not sometimes one or the other. Quote Link to post Share on other sites
justsomeguy 1,135 Posted August 19, 2019 Report Share Posted August 19, 2019 I would use ord or mb_ord to loop through the characters in the string and print the value of each byte to see what's actually there. https://www.php.net/manual/en/function.ord.php https://www.php.net/manual/en/function.mb-ord.php Quote Link to post Share on other sites
Mudsaf 17 Posted August 20, 2019 Author Report Share Posted August 20, 2019 (edited) On 8/19/2019 at 8:57 PM, justsomeguy said: I would use ord or mb_ord to loop through the characters in the string and print the value of each byte to see what's actually there. https://www.php.net/manual/en/function.ord.php https://www.php.net/manual/en/function.mb-ord.php The unicode block symbol returned value of 194 via ord() function, got any idea what might be the cause to create that unicode block? Source code of tester below, string too. <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <style> #kool { width:800px; border:groove; margin:0 auto; padding:25px; } .mid { margin:0 auto; max-width:800px; text-align:centeR; margin-top:10px; } #preg_input { width:300px; } </style> </head> <body> <?php /* Test preg_replace function */ // #%&()=@£\$€\[\]_\-,.:? $basic = "A-Za-Z0-9"; $preg_pattern = "/[^A-Za-z0-9!\"#%£&()=@\s]/"; $func_preg_replace = preg_replace($preg_pattern,"",$_GET['preg']); if (isset($_GET['preg'])) { echo "<div id='kool'>"; echo "<b>Original string: </b> " . $_GET['preg']; echo "<hr>"; echo "<b>Preg pattern: </b> " . $preg_pattern; echo "<hr>"; echo "<b>Result: </b> " . $func_preg_replace; echo "<hr>"; echo "<b>Rawurl: </b> " . rawurlencode($_GET['preg']); echo "<hr>"; echo "<b>Ord: </b> "; for ($i=0;$i<strlen($func_preg_replace);$i++) { echo $func_preg_replace[$i] . "(" . ord($func_preg_replace[$i]) . ") "; } echo "</div>"; } ?> <div class="mid"> <form method="get" action="preg_test.php"> <input type="text" id="preg_input" name="preg" <?php if (isset($_GET['preg'])) { echo " value='" . $_GET['preg'] . "'"; } ?>> <input type="submit"> </form> </div> </body> </html> And the string without What is love 0-9, Specialcharacters: !"#¤%&/()=? Does it reproduce for you guys? ---- Also tried to UTF-8 encode string via php, the preg_replace string, 2 new unicode blocks appeared with �(195) �(130). $func_preg_replace = utf8_encode ($func_preg_replace); Edited August 20, 2019 by Mudsaf Quote Link to post Share on other sites
justsomeguy 1,135 Posted August 21, 2019 Report Share Posted August 21, 2019 What about the original string? Is the 194 part of a code point that only gets partially replaced? Quote Link to post Share on other sites
Mudsaf 17 Posted August 21, 2019 Author Report Share Posted August 21, 2019 Original string returns 2 unicode blocks (straight from $_GET): �(194) �(164). Where ¤ is. Quote Link to post Share on other sites
justsomeguy 1,135 Posted August 21, 2019 Report Share Posted August 21, 2019 Add the u modifier to your pattern: /[^A-Za-z0-9!\"#%£&()=@\s]/u 1 Quote Link to post Share on other sites
Mudsaf 17 Posted August 21, 2019 Author Report Share Posted August 21, 2019 7 minutes ago, justsomeguy said: Add the u modifier to your pattern: /[^A-Za-z0-9!\"#%£&()=@\s]/u Not sure what kind of sorcery is this, but it works now. Thank you! Quote Link to post Share on other sites
justsomeguy 1,135 Posted August 21, 2019 Report Share Posted August 21, 2019 From the manual: u (PCRE_UTF8) This modifier turns on additional functionality of PCRE that is incompatible with Perl. Pattern and subject strings are treated as UTF-8. An invalid subject will cause the preg_* function to match nothing; an invalid pattern will trigger an error of level E_WARNING. Five and six octet UTF-8 sequences are regarded as invalid since PHP 5.3.4 (resp. PCRE 7.3 2007-08-28); formerly those have been regarded as valid UTF-8. 1 Quote Link to post Share on other sites
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.