Jump to content

str_replace not returning expected value


jarvis

Recommended Posts

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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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 by ginerjm
Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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})

Link to comment
Share on other sites

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 by mac_gyver
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.)

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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 by mac_gyver
  • Great Answer 1
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.