How To Remove All Occurrences Of C2a0 In A String With PHP?

Just browsing Stack Overflow? Help us improve your experience. Sign up for research
    1. Home
    2. Questions
    3. Tags
    4. Users
    5. Companies
    6. Labs
    7. Jobs
    8. Discussions
    9. Collectives
    10. Communities for your favorite technologies. Explore all Collectives

  1. Teams

    Ask questions, find answers and collaborate at work with Stack Overflow for Teams.

    Try Teams for free Explore Teams
  2. Teams
  3. Ask questions, find answers and collaborate at work with Stack Overflow for Teams. Explore Teams

Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

Get early access and see previews of new features.

Learn more about Labs How to remove all occurrences of c2a0 in a string with PHP? Ask Question Asked 13 years, 1 month ago Modified 5 years, 11 months ago Viewed 18k times Part of PHP Collective 21

I'm working with a CSV file which is exported from Excel.

I have a column that contains a value of 1 234,00. I need to get all whitespaces away from these kinds of columns with PHP and I've tried to do it with preg_replace("/\s*/","",$column) as well as with str_replace(" ","",$column). I was almost ready to lose it so I took a glance into the csv-file with a HEX-editor and noticed, that this space consist of two hex values, C2 and A0 which seems to be UTF-8 non-breaking space.

But I suck with encoding stuff and I'm still confused in finding a way to remove them. Any ideas?

Share Improve this question Follow asked Oct 27, 2011 at 23:01 BudwiseЯ's user avatar BudwiseЯBudwiseЯ 1,8262 gold badges16 silver badges28 bronze badges Add a comment |

2 Answers 2

Sorted by: Reset to default Highest score (default) Trending (recent votes count more) Date modified (newest first) Date created (oldest first) 48 $column = str_replace("\xc2\xa0", '', $column); Share Improve this answer Follow answered Oct 27, 2011 at 23:04 phihag's user avatar phihagphihag 287k75 gold badges467 silver badges482 bronze badges 3
  • To whom it may be interested this works because the input is UTF-8, if the input is ISO-8895-1 or CP1252 you must use str_replace("\xA0", '', $column); Thanks to stackoverflow.com/a/33019796/260080 – Marco Demaio Commented Feb 10, 2019 at 21:19
  • @MarcoDemaio Or better first, convert to UTF-8 first. Any application that works with ISO-8859-1 internally is buggy – for instance, it will not correctly handle emojis. – phihag Commented Feb 11, 2019 at 7:45
  • Beware that if $column was null, it will become an empty string. – at54321 Commented Aug 2, 2021 at 6:52
Add a comment | 17

You may use trim

trim($data['value'], " \t\n\r\0\x0B\xc2\xa0")

Where \t\n\r\0\x0B is defualt mask, \xc2\xa0 need add

Share Improve this answer Follow edited Dec 3, 2018 at 14:05 answered Apr 7, 2015 at 11:38 Andrey Vorobyev's user avatar Andrey VorobyevAndrey Vorobyev 8861 gold badge10 silver badges37 bronze badges 2
  • 2 trim also includes space in the default mask — " \t\n\r\0\x0B\xc2\xa0". – rhs Commented Mar 5, 2016 at 1:52
  • 1 This might remove parts of an otherwise correct multibyte character. var_dump("\xc2\xb4AAAA"); var_dump(trim("\xc2\xb4AAAA", " \t\n\r\0\x0B\xc2\xa0")); – 0x6368 Commented Nov 7, 2019 at 17:26
Add a comment |

Your Answer

Reminder: Answers generated by artificial intelligence tools are not allowed on Stack Overflow. Learn more

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid …

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.

Draft saved Draft discarded

Sign up or log in

Sign up using Google Sign up using Email and Password Submit

Post as a guest

Name Email

Required, but never shown

Post Your Answer Discard

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.

PHP Collective Join the discussion This question is in a collective: a subcommunity defined by tags with relevant content and experts.
  • The Overflow Blog
  • Your docs are your infrastructure
  • Featured on Meta
  • More network sites to see advertising test [updated with phase 2]
  • We’re (finally!) going to the cloud!
  • Call for testers for an early access release of a Stack Overflow extension...
Visit chat

Linked

10 MySQL or PHP is appending a  whenever the £ is used -1 How to remove NBSP? 0 Removal of special characters, a comma separated text, PHP 0 character convertion with CSV file 2 php to csv and special characters 0 Remove a line with specific characters in a variable before putting into CSV 0 find character in string from csv file using php? 1 Targeted string replace in csv file php 3 special character to csv in php 0 Remove characters in array from file CSV PHP 1 How to remove duplicates from a PHP string separated by "X"? 0 how to clean a dirty csv string using php regex

Hot Network Questions

  • How to say "Each one of the following" in German?
  • Is there a better way to implement `\ifblank` condtion check when there are multiple option arguments with default values in a command definition
  • Need help to prove the summation of series. It works in programming, but I don't know how to prove.
  • How to sample a single point from a volume
  • Do longer papers have lower chances of being accepted because they take up more "space" in a journal issue (STEM)
  • Why did General Groves mention the baby delivery count here?
  • Spellcheck I Before E
  • How can I fix this leaking connection in the heating system?
  • Quantum entanglement explained by non-local hidden variables when photons are not the propagators of information?
  • In a Frequentist setting, how are we able to condition on the null hypothesis being True/False?
  • How to Classify Driving Behaviors (Acceleration, Braking, Turning) Using 2D Coordinates and Velocity?
  • Is there any penalty for providing half cover to another creature?
  • What would T-Rex cavalry be used for?
  • Why is Anarchism not considered fundamentally against the "democratic order" in Germany?
  • Path from plane
  • Is there a symbol for the Hyper key?
  • Study("somewhere")
  • What is the complexity of modulo order-finding problem on classical computer?
  • Table structure with multiple foreign keys and values
  • What is small arch between two notes and how to play it?
  • Configure Linux to regularly sync cached data to disk
  • Can I make a pipe save data to disk instead of blocking until the reader can continue?
  • How to break temporarily from loop in repeat zone
  • Is the A321 XLR really necessary to fly MAD-BOS?
more hot questions Question feed Subscribe to RSS Question feed

To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

lang-php

Từ khóa » C2 A0 Php