techboy999 Posted May 30, 2017 Share Posted May 30, 2017 Hello guys I sitting here and try to get a string in to links, I am almost there just problem I have is that some special characters will not link to the link I need get the the preg_replace to use UTF-8, I know it's possible with a parameter /u, but I cant find out where to place it Here is the code: $str = $message4; // Url Template $template = '<a href="search.php?q=%1$s">%1$s</a>'; // Replace Words echo preg_replace("/(?!(?:[^<]+>|[^>]+<\/a>))\b([a-å]+)\b/is", sprintf($template, "\\1"), $str); Quote Link to comment https://forums.phpfreaks.com/topic/304034-preg_replace-utf-8/ Share on other sites More sharing options...
requinix Posted May 30, 2017 Share Posted May 30, 2017 Before that, [a-å]+will not work: character ranges work on bytes (or Unicode codepoints) and that range will cover far more than just letters. So you're trying to link all words. I'd go for [\pL']+to match letters and apostrophes. As for getting Unicode mode, that's the /u flag. "Flags" go after the ending delimiter - you're using two of them already... Quote Link to comment https://forums.phpfreaks.com/topic/304034-preg_replace-utf-8/#findComment-1546914 Share on other sites More sharing options...
techboy999 Posted May 30, 2017 Author Share Posted May 30, 2017 Hi It works almost with the [\pL']+ but I have 2 words that always not link the special characters the words are så and må and also også, you have any idea to fix that ? Thanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/304034-preg_replace-utf-8/#findComment-1546915 Share on other sites More sharing options...
requinix Posted May 30, 2017 Share Posted May 30, 2017 It should work fine. Are you sure the text is in UTF-8 encoding? What does bin2hex($str) show? Quote Link to comment https://forums.phpfreaks.com/topic/304034-preg_replace-utf-8/#findComment-1546916 Share on other sites More sharing options...
techboy999 Posted May 30, 2017 Author Share Posted May 30, 2017 (edited) ?476f646d6f7267656e2048e56265722064752068617220736f76657420676f6474206f67206472f86d74204e6f676c652046616e7461736b7469736b65206472f86d6d652c2048617220647520696b6b652065742062696c6c656465206a6567206de52073652073f86465206f67206876616420657220646974206e61766e206f672068766f7220657220647520667261203f some cryptic Edited May 30, 2017 by techboy999 Quote Link to comment https://forums.phpfreaks.com/topic/304034-preg_replace-utf-8/#findComment-1546917 Share on other sites More sharing options...
requinix Posted May 30, 2017 Share Posted May 30, 2017 That's not UTF-8. Probably ISO 8859-1 but I can't tell for sure right now. Quote Link to comment https://forums.phpfreaks.com/topic/304034-preg_replace-utf-8/#findComment-1546918 Share on other sites More sharing options...
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.