Home > database >  Remove empty spaces
Remove empty spaces

Time:12-12

I got problem with corrupted CSV, that has empty spaces in image links.

CSV file is automaticly created from my clients business partner and is ~22000 lines big. Around 30 links in CSV are broken and I can't fix it. As it's updating every 3 hours.

So, I'm trying to come up with an automatic solution, but nothing seems to work.

This is error that I get: Image

Problem is in image links, like this one: https://images.tyroo.de/GISLAVED_ULTRA_SPEED2�.jpg

It has space before .jpg some files got 3 spaces, some got 7 spaces. If I fix them manually in CSV file, it works, but I need to find a php automatic way to do that.

This is how I get this variable from CSV file: $image_link = $csv[$i][52];

And I have tried these ways to remove space before .jpg, but none of them delete empty spaces.

$item_image_link = preg_replace('/\s /', '', $image_link);
$item_image_link = preg_replace('/\s\s /', ' ', $image_link);
$item_image_link = str_replace(" ", "", $image_link);
$item_image_link = preg_replace("/\s /", "", $image_link);

CodePudding user response:

As mentioned your issue is due to encoding. Either request that the csv is provided in utf8, or attempt to covert it yourself.

You could try

$image_link = utf8_encode($csv[$i][52]);

Or using mb_string if you know the original encoding

$image_link = mb_convert_encoding($csv[$i][52], "SomeEncodingType", "UTF-8");

I'm not sure but it may be too late at this point. Instead you can try and decode the raw csv string before its passed to whatever php function you are using to break it into the $csv array.

  • Related