[GH-ISSUE #216] Spotify API - why can't I detect multiplace spaces in array data? #148

Closed
opened 2026-02-27 19:26:19 +03:00 by kerem · 2 comments
Owner

Originally created by @ghost on GitHub (Jan 21, 2021).
Original GitHub issue: https://github.com/jwilsson/spotify-web-api-php/issues/216

Hi Jonathan!

I hope you're well.

I'm using the search method like this:

// @@@@@@@@@@@@@@@ CALL THE API @@@@@@@@@@@@@@@

$options = ['return_assoc' => true];
$api = new SpotifyWebAPI\SpotifyWebAPI($options);	
$api->setAccessToken($accessToken);

// @@@@@@@@@@@@@@@ search @@@@@@@@@@@@@@@

try {
    $search_options = ['offset' => 0,'limit' => $limit];
    $search = $api->search("trees","episode",$search_options);
} catch (SpotifyWebAPI\SpotifyWebAPIException $e) {
	header('Location:index.php');
	exit;
}

That works fine and as 'return_assoc' => true is set, then the data that is returned is in the form of an array.

Data returned in the episode description (e.g. $episodes['items'][0]['description']) can have two spaces in it, which I need to detect so I can replace those with a <br /> tag.

The issue I have is that I cannot detect those double spaces when the data is returned by the API.

I have a function to replace 2 spaces with 44 for testing, and it works fine if I hard-code the string values - e.g.

$arr = [];
array_push($arr, "but  this one does  have some double spaces");
array_push($arr, "Welcome back, sleepyheads. Tonight, Tom tells the story of two siblings who find wonder and adventure in their new treehouse.  Thank you so much for listening!  Feedback? Let us know your thoughts!");
$str = "this also  has some double spaces";

$array_element_1 = 	$arr[0];
$array_element_2 = 	$arr[1];

$array_element_1 = 	SwapDouble($array_element_1);
$array_element_2 = 	SwapDouble($array_element_2);
$str_checked = 		SwapDouble($str);

The above returns:

>> but44this one does44have some double spaces
>> Welcome back, sleepyheads. Tonight, Tom tells the story of two siblings who find wonder and adventure in their new treehouse.44Thank you so much for listening!44Feedback? Let us know your thoughts!
>> this also44has some double spaces

However, if I get the string value from the API - e.g.

$array_episodes = $search_array['episodes']['items'];
foreach($array_episodes as $episode) {
	$episode_desc =	$episode['description'];
	$episode_desc =	preg_replace("/ {2,}/", "44", $episode_desc);
}

Then the double spaces are not detected.

I can't check using the official Spotify API (e.g. using PHP and their API) - I only know how to use the API via your (excellent) library.

I'm not sure if this is a Spotify issue, or a library issue, or a PHP issue?

Any advice much appreciated.

Thanks!

Jim

Originally created by @ghost on GitHub (Jan 21, 2021). Original GitHub issue: https://github.com/jwilsson/spotify-web-api-php/issues/216 Hi Jonathan! I hope you're well. I'm using the search method like this: // @@@@@@@@@@@@@@@ CALL THE API @@@@@@@@@@@@@@@ $options = ['return_assoc' => true]; $api = new SpotifyWebAPI\SpotifyWebAPI($options); $api->setAccessToken($accessToken); // @@@@@@@@@@@@@@@ search @@@@@@@@@@@@@@@ try { $search_options = ['offset' => 0,'limit' => $limit]; $search = $api->search("trees","episode",$search_options); } catch (SpotifyWebAPI\SpotifyWebAPIException $e) { header('Location:index.php'); exit; } That works fine and as `'return_assoc' => true` is set, then the data that is returned is in the form of an array. Data returned in the episode description (e.g. `$episodes['items'][0]['description']`) can have two spaces in it, which I need to detect so I can replace those with a `<br />` tag. The issue I have is that I cannot detect those double spaces when the data is returned by the API. I have a function to replace 2 spaces with `44` for testing, and it works fine if I hard-code the string values - e.g. $arr = []; array_push($arr, "but this one does have some double spaces"); array_push($arr, "Welcome back, sleepyheads. Tonight, Tom tells the story of two siblings who find wonder and adventure in their new treehouse. Thank you so much for listening! Feedback? Let us know your thoughts!"); $str = "this also has some double spaces"; $array_element_1 = $arr[0]; $array_element_2 = $arr[1]; $array_element_1 = SwapDouble($array_element_1); $array_element_2 = SwapDouble($array_element_2); $str_checked = SwapDouble($str); The above returns: ``` >> but44this one does44have some double spaces >> Welcome back, sleepyheads. Tonight, Tom tells the story of two siblings who find wonder and adventure in their new treehouse.44Thank you so much for listening!44Feedback? Let us know your thoughts! >> this also44has some double spaces ``` However, if I get the string value from the API - e.g. $array_episodes = $search_array['episodes']['items']; foreach($array_episodes as $episode) { $episode_desc = $episode['description']; $episode_desc = preg_replace("/ {2,}/", "44", $episode_desc); } Then the double spaces are not detected. I can't check using the official Spotify API (e.g. using PHP and their API) - I only know how to use the API via your (excellent) library. I'm not sure if this is a Spotify issue, or a library issue, or a PHP issue? Any advice much appreciated. Thanks! Jim [1]: https://github.com/jwilsson/spotify-web-api-php
kerem 2026-02-27 19:26:19 +03:00
  • closed this issue
  • added the
    question
    label
Author
Owner

@jwilsson commented on GitHub (Jan 21, 2021):

Hey Jim!
This is really strange, when I try it it's correctly replacing double spaces. Some things off the top of my head which you can try, though:

  • Rewrite the preg_replace part from scratch, sometimes invisible garbage characters sneak in.
  • Replace the space in preg_replace with \s which will match all space characters (including tabs and newlines), i.e. preg_replace("/\s{2,}", "44", $episode_desc);.
  • Use str_replace(" ", "44", $episode_desc) instead, should be faster in this case as well (note the double spaces although GitHub seems to remove them).
  • Make sure your file is saved as UTF-8 so there's no text encoding mismatches.

I don't think it's an API or library issue, but probably something with the text encoding or similiar.

Hope this helps!

Cheers,
Jonathan

<!-- gh-comment-id:764914996 --> @jwilsson commented on GitHub (Jan 21, 2021): Hey Jim! This is really strange, when I try it it's correctly replacing double spaces. Some things off the top of my head which you can try, though: - Rewrite the `preg_replace` part from scratch, sometimes invisible garbage characters sneak in. - Replace the space in `preg_replace` with `\s` which will match all space characters (including tabs and newlines), i.e. `preg_replace("/\s{2,}", "44", $episode_desc);`. - Use `str_replace(" ", "44", $episode_desc)` instead, should be faster in this case as well (note the double spaces although GitHub seems to remove them). - Make sure your file is saved as UTF-8 so there's no text encoding mismatches. I don't think it's an API or library issue, but probably something with the text encoding or similiar. Hope this helps! Cheers, Jonathan
Author
Owner

@ghost commented on GitHub (Jan 22, 2021):

Hi Jonanthan - thanks for your reply.

I think the issue is probably caused by the fact I'm running the code on IIS on Windows 10.

If I had access to be able to run it on Apache that's running on e.g. Linux, I imagine it'd work okay too.

I have Apache installed on my PC as well, and the same issue happens when I run the code on PHP on Apache too.

I tried your helpful suggestions - thank you - but nothing works.

I guess it's just a limitation of using Windows to deliver PHP content :-)

Thanks again

Jim

<!-- gh-comment-id:765211932 --> @ghost commented on GitHub (Jan 22, 2021): Hi Jonanthan - thanks for your reply. I think the issue is probably caused by the fact I'm running the code on IIS on Windows 10. If I had access to be able to run it on Apache that's running on e.g. Linux, I imagine it'd work okay too. I have Apache installed on my PC as well, and the same issue happens when I run the code on PHP on Apache too. I tried your helpful suggestions - thank you - but nothing works. I guess it's just a limitation of using Windows to deliver PHP content :-) Thanks again Jim
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/spotify-web-api-php#148
No description provided.