jarvis Posted August 8, 2022 Share Posted August 8, 2022 Good Morning, I'm trying to do a str_replace on a value but it doesn't seem to return the result I'm after. I have $child_icon which has the value of "\\f192" What I'm trying to do, is change it to \u{f192 So thought str_replace would be the best route. If I use str_replace('\\', '', $child_icon); It returns f192, as I would expect. However, I can't seem to add \u{ at the beginning. str_replace('\\', '\u{', $child_icon); Returns \\u{f192 str_replace('\\', '\\u{', $child_icon); Returns \\u{f192 If I try and set the value as a variable, then amend it, it still isn't right, for example: $c_icon = str_replace('\\', '', $child_icon); $icon = '\u{f'.$c_icon; It still returns \\u{f192 Am guessing I'm missing something very obvious. Is there another way to do this? Any help would be very much appreciated! Thanks Quote Link to comment https://forums.phpfreaks.com/topic/315152-str_replace-not-returning-expected-value/ Share on other sites More sharing options...
ginerjm Posted August 8, 2022 Share Posted August 8, 2022 I"m gonna take a stab at this. Since the backslash is a common 'escape' key, try replacing the double backslash with a triple one. Quote Link to comment https://forums.phpfreaks.com/topic/315152-str_replace-not-returning-expected-value/#findComment-1599135 Share on other sites More sharing options...
Barand Posted August 8, 2022 Share Posted August 8, 2022 perhaps ? echo str_replace("\\", "\\u{", "\\f192"); //--> \u{f192 Quote Link to comment https://forums.phpfreaks.com/topic/315152-str_replace-not-returning-expected-value/#findComment-1599137 Share on other sites More sharing options...
ginerjm Posted August 8, 2022 Share Posted August 8, 2022 Harder than I thought. How's this? $val = '\\f192'; while(substr($val,0,1) == '\\') $val = substr($val,1); $result = '\\u{' . $val; echo "Result is $result"; Quote Link to comment https://forums.phpfreaks.com/topic/315152-str_replace-not-returning-expected-value/#findComment-1599139 Share on other sites More sharing options...
ginerjm Posted August 8, 2022 Share Posted August 8, 2022 Of course now that I have provided a somewhat clumsy approach to answering your question I have to ask - Why? Where are you getting this value that is prone to misunderstanding because of the double slashes? Is this homework? Quote Link to comment https://forums.phpfreaks.com/topic/315152-str_replace-not-returning-expected-value/#findComment-1599140 Share on other sites More sharing options...
jarvis Posted August 8, 2022 Author Share Posted August 8, 2022 Many thanks for the replies. Sadly, none of those seem to work. In answer to @ginerjm, the value is being pulled from a database field ($child_icon) It's part of conditional: if (!empty($child_icon)) : $pin_color = $child_pin_colour; $icon_color = $child_icon_colour; $icon = $c_icon; elseif (!empty($parent_icon)) : $pin_color = $parent_pin_colour; $icon_color = $parent_icon_colour; $icon = $p_icon; else: $pin_color = '#f76458'; $icon_color = '#000'; $icon = "\u{f57f}"; endif; This is then used in a loop to construct some json: $json[] = array( 'lat' => $location['lat'], 'lon' => $location['lng'], 'title' => $location_title, 'html' => $html, 'pin_color' => $pin_color, 'icon_color'=> $icon_color, 'icon' => $icon ); Which is then used in the JavaScript: var loc = { 'lat' : data.lat, 'lon' : data.lon, 'html' : data.html, 'icon': { path: MAP_PIN, fillColor: data.pin_color, fillOpacity: 1, strokeColor: '', strokeWeight: 0 }, 'label': { fontFamily: 'Fontawesome', text: data.icon, fontSize: '18px', color: data.icon_color, className: 'map-label' }, }; markers.locations.push(loc); Which then outputs custom map markers on a Google Map. However, no matter what I try, the output always seems to include a double slash: "pin_color":"#dd9933","icon_color":"#ffffff","icon":"\\u{f192}" Yet needs to be: "pin_color":"#dd9933","icon_color":"#ffffff","icon":"\u{f192}" Apologies if you needed the bigger picture from the outset!? But wasn't sure it was needed. With hindsight, perhaps it would help and may have other ways to solve it? Quote Link to comment https://forums.phpfreaks.com/topic/315152-str_replace-not-returning-expected-value/#findComment-1599142 Share on other sites More sharing options...
ginerjm Posted August 8, 2022 Share Posted August 8, 2022 (edited) I beg to differ. What you asked for is being produced by my code when I run it. $val = "\\f192"; while(substr($val,0,1) == '\\') $val = substr($val,1); $result = '\\u{' . $val; echo "Result is now: $result"; You say these are database values. Are the extra slashes perhaps part of an addslashes function put there when the data was saved and needs to be stripped out when it is read back in? PS I don't see the use of $child_icon anywhere else but the very first line of your example. How then does it impact your code later on? Edited August 8, 2022 by ginerjm Quote Link to comment https://forums.phpfreaks.com/topic/315152-str_replace-not-returning-expected-value/#findComment-1599143 Share on other sites More sharing options...
jarvis Posted August 8, 2022 Author Share Posted August 8, 2022 Thanks @ginerjm Apologies, wasn't doubting your code at all. Ok, so the database stores the data like so: { "style" : "regular", "id" : "circle-dot", "label" : "Circle dot", "unicode" : "f192" } If I output (echo) the unicode value (which is the one I need), it always returns the value as: \f192 If I check the JSON being created, the value already changes to: "pin_color":"#dd9933","icon_color":"#ffffff","icon":"\\f192" I only have the code as per the above, so something is clearly causing an issue. With regards to the '$child_icon anywhere else but the very first line of your example', this is fine. Basically, the code has 3 levels it checks through: child, parent then default This is a Wordpress site you see and the values are stored against taxonomies. It checks the child value, if it has a value it's displayed, if it's empty, it checks the parent value and displays it, if that's also empty, it displays the default. Only the initial code needs this, as it determines which value is passed through the JSON and therefore displayed on the map - I possibly haven't explained that very well! Quote Link to comment https://forums.phpfreaks.com/topic/315152-str_replace-not-returning-expected-value/#findComment-1599145 Share on other sites More sharing options...
mac_gyver Posted August 8, 2022 Share Posted August 8, 2022 this appears like it is a mutli-byte Unicode value. it is being escaped by the json_encode() statement. to get this to not happen, add the JSON_UNESCAPED_UNICODE flag as the 2nd call-time parameter in the json_encode() call. Quote Link to comment https://forums.phpfreaks.com/topic/315152-str_replace-not-returning-expected-value/#findComment-1599146 Share on other sites More sharing options...
jarvis Posted August 8, 2022 Author Share Posted August 8, 2022 Thanks @mac_gyver I've tried both: $json_data = json_encode($json,JSON_UNESCAPED_UNICODE); And $json_data = json_encode($json,JSON_UNESCAPED_SLASHES); In both instances, it still seems to return: "pin_color":"#dd9933","icon_color":"#ffffff","icon":"\\f192" This is before I even try to do anything to get the value into the format I need (\u{f192}) Quote Link to comment https://forums.phpfreaks.com/topic/315152-str_replace-not-returning-expected-value/#findComment-1599147 Share on other sites More sharing options...
mac_gyver Posted August 8, 2022 Share Posted August 8, 2022 (edited) for the else: logic, with the literal value being assigned to the $icon variable, does it work as expected? does this only not work for database values in $c_icon or $p_icon? btw - JSON_UNESCAPED_SLASHES has to do with / not \ characters. Edited August 8, 2022 by mac_gyver Quote Link to comment https://forums.phpfreaks.com/topic/315152-str_replace-not-returning-expected-value/#findComment-1599148 Share on other sites More sharing options...
jarvis Posted August 8, 2022 Author Share Posted August 8, 2022 Thanks @mac_gyver I see RE the JSON_UNESCAPED_SLASHES If I simply set the code to: $pin_color = '#f76458'; $icon_color = '#000'; $icon = "\u{f57f}"; Then the return data is: "pin_color":"#f76458","icon_color":"#000","icon":"\uf57f" Which works. If I amend the code to: if (!empty($child_icon)) : $pin_color = $child_pin_colour; $icon_color = $child_icon_colour; $icon = "\u{f57f}"; elseif (!empty($parent_icon)) : $pin_color = $parent_pin_colour; $icon_color = $parent_icon_colour; $icon = "\u{f57f}"; else: $pin_color = '#f76458'; $icon_color = '#000'; $icon = "\u{f57f}"; endif; This also works, showing the different pin/marker colours and the same icon. It just seems to be when getting the value from the DB for $icon it doesn't work. Quote Link to comment https://forums.phpfreaks.com/topic/315152-str_replace-not-returning-expected-value/#findComment-1599149 Share on other sites More sharing options...
mac_gyver Posted August 8, 2022 Share Posted August 8, 2022 where did the values in the database originally come from, how exactly were they put into the database table (database extension, any _escape_string functions, any prepared queries), and under what php version? i suspect that the values were double-escaped when they were put into the database and that what appears like a single \ when being echoed is actually two \\ in the values. to check this, see what the 'view source' in the browser of an echoed database value is (without any json_encoding applied yet.) Quote Link to comment https://forums.phpfreaks.com/topic/315152-str_replace-not-returning-expected-value/#findComment-1599150 Share on other sites More sharing options...
jarvis Posted August 8, 2022 Author Share Posted August 8, 2022 The site is a Wordpress site and uses a plugin. Basically, it adds a dropdown of fontawesome icons against the taxonomy field. You select the icon, click save and the data is stored in the termemta table. The meta_key is icon and the meta_value looks like: { "style" : "regular", "id" : "circle-dot", "label" : "Circle dot", "unicode" : "f192" } It would make sense to escape the data when storing. If I access the value direct from the database, it shows as: f192 It therefore seems to be once the code is passed to the encoding aspect that it throws a wobbly and adds the slashes Quote Link to comment https://forums.phpfreaks.com/topic/315152-str_replace-not-returning-expected-value/#findComment-1599151 Share on other sites More sharing options...
mac_gyver Posted August 8, 2022 Share Posted August 8, 2022 i can tell you why this doesn't work, but cannot currently tell you a way of making it work. the double-quoted "\u{value}" escape sequence only works for literal values. you cannot use a php variable to supply the value. you could use the evil eval() to do this, but don't. Quote Link to comment https://forums.phpfreaks.com/topic/315152-str_replace-not-returning-expected-value/#findComment-1599155 Share on other sites More sharing options...
jarvis Posted August 8, 2022 Author Share Posted August 8, 2022 (edited) @mac_gyver Yes, it seems to be a right pain and cannot for the life of me find a solution - which is most annoying!! Thanks again Edited August 8, 2022 by jarvis Read up on eval() Quote Link to comment https://forums.phpfreaks.com/topic/315152-str_replace-not-returning-expected-value/#findComment-1599156 Share on other sites More sharing options...
mac_gyver Posted August 8, 2022 Share Posted August 8, 2022 (edited) after searching and experimenting, both of the following methods will convert a dynamic unicode code point (the f192, f57f, ... values) to utf8 - $icon = IntlChar::chr(hexdec($c_icon)); // or $icon = mb_chr(hexdec($c_icon), 'UTF-8'); replace the $c_icon with $p_icon, or the literal 'f57f' in the else: branch. this requires removing the JSON_UNESCAPED_UNICODE flag in the json_encode() call. Edited August 8, 2022 by mac_gyver 1 Quote Link to comment https://forums.phpfreaks.com/topic/315152-str_replace-not-returning-expected-value/#findComment-1599158 Share on other sites More sharing options...
jarvis Posted August 8, 2022 Author Share Posted August 8, 2022 OMG @mac_gyver Thank you so much (and others who read and commented). I can indeed confirm that works! Honestly, I thought I was going mad but that's absolutely brilliant! Thank you again so much, it truly is very much appreciated! Quote Link to comment https://forums.phpfreaks.com/topic/315152-str_replace-not-returning-expected-value/#findComment-1599159 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.