How can I explode and trim whitespace

0 votes

For example, I would like to create an array from the elements in this string:

$str = 'red,     green,     blue ,orange';

I know you can explode and loop through them and trim:

$arr = explode(',', $str);
foreach ($arr as $value) {
    $new_arr[] = trim($value);
}

But I feel like there's a one line approach that can handle this. Any ideas?

Oct 1, 2020 in PHP by kartik
• 37,510 points
5,056 views

1 answer to this question.

0 votes

Hello @kartik,

You can do the following using array_map:

$new_arr = array_map('trim', explode(',', $str));

Hope it helps!!
Thank you!!

answered Oct 1, 2020 by Niroj
• 82,880 points

Related Questions In PHP

0 votes
1 answer

How can I include a php file and also send query parameters in php?

Hello, You could do something like this to ...READ MORE

answered Nov 10, 2020 in PHP by Niroj
• 82,880 points
5,679 views
0 votes
0 answers

How can I sort arrays and data in PHP?

How do I sort an array in ...READ MORE

May 29, 2022 in PHP by Kichu
• 19,050 points
194 views
0 votes
0 answers

What Does 6k views mean and how can I format the number in PHP?

What does "6k views" mean and how ...READ MORE

Jun 2, 2022 in PHP by Kichu
• 19,050 points
320 views
0 votes
0 answers

How can I store and retrieve images from a MySQL database using PHP?

How can I insert an image in ...READ MORE

Jun 14, 2022 in PHP by narikkadan
• 63,420 points
358 views
0 votes
1 answer

jQuery AJAX fires error callback on window unload - how do I filter out unload and only catch real errors?

Hello, In the error callback or $.ajax you have three ...READ MORE

answered Apr 27, 2020 in Java-Script by Niroj
• 82,880 points
3,711 views
0 votes
1 answer

How do I pass command line arguments to a Node.js program?

Hello @kartik, If your script is called myScript.js ...READ MORE

answered May 5, 2020 in Java-Script by Niroj
• 82,880 points
2,919 views
0 votes
1 answer

Error:Issue when trying to use IN() in wordpress database

Hello @kartik, Try this code : // Create an ...READ MORE

answered May 8, 2020 in PHP by Niroj
• 82,880 points
830 views
+2 votes
1 answer

How do I debug Node.js applications?

Hello @kartik, Use node-inspector  from any browser supporting WebSocket. Breakpoints, ...READ MORE

answered Jul 8, 2020 in Node-js by Niroj
• 82,880 points
772 views
0 votes
1 answer

How can I use Sockets.io on the client side and communicate with a PHP based application on the server?

Hello @kartik, For 'long-lived connection' , you can ...READ MORE

answered Aug 24, 2020 in PHP by Niroj
• 82,880 points
2,698 views
0 votes
1 answer

How can I select and upload multiple files with HTML and PHP using HTTP POST?

Hello @kartik, This is possible in HTML5. Example (PHP ...READ MORE

answered Sep 15, 2020 in PHP by Niroj
• 82,880 points
1,641 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP