Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7410695
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T06:16:22+00:00 2026-05-29T06:16:22+00:00

I’m processing a text file output by a scientific instrument. I do not have

  • 0

I’m processing a text file output by a scientific instrument. I do not have documentation about how the file is produced. But I’ve discovered that it is full of invisible characters and characters that look normal but aren’t. I read the file into an array and try to clean it up. Here’s my process (showing only the first 4 lines of the file).

$datarr=file($_FILES['gcfile']['tmp_name'],FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);

$datarr before processing

(note BOM, EOL is still there, length of strings is too large):

array(4) {
  [0]=>
  string(168) "ÿþC:\CHEM32\2\DATA\20120120 DA KLR\20120120 DA KLR 2012-01-20 09-55-35\P21K1000001.D
"
  [1]=>
  string(33) "tryptophan + DA
"
  [2]=>
  string(55) "Number of Peaks found    2
"
  [3]=>
  string(63) ""  1  ,  13.08 ,       36.29 "
"
}

processing

$datarr[0]=removeBOM($datarr[0]);//remove byte order mark at beginning of file

$options=array(FILTER_FLAG_STRIP_HIGH, FILTER_FLAG_STRIP_LOW);
$patterns=array('/\pC/','/\'/', '/\"/');

array_walk($datarr,function(&$v) use($options, $patterns){
    $v=filter_var($v,FILTER_SANITIZE_STRING, $options);
    $v=trim(preg_replace($patterns,'',$v));
});

after processing

(note retention of double quotes on $datarr[3], length of strings ~= visible length, BOM gone)

array(4) {
  [0]=>
  string(82) "C:\CHEM32\2\DATA\20120120 DA KLR\20120120 DA KLR 2012-01-20 09-55-35\P21K1000001.D"
  [1]=>
  string(15) "tryptophan + DA"
  [2]=>
  string(26) "Number of Peaks found    2"
  [3]=>
  string(38) ""  1  ,  13.08 ,       36.29 ""

$datarr[3], though much improved, still has a reported length greater than it’s visible length, and the ” marks weren’t removed. If I output the string as ascii numbers:

$l=strlen($datarr[3]);
for($i=0;$i<$l;$i++){
    echo ord($datarr[3][$i]), ", ";
}
echo PHP_EOL;
$x= '"  1  ,  13.08 ,       36.29 "
';//copied from webpage output
$l=strlen($x);
for($i=0;$i<$l;$i++){
    echo ord($x[$i]), ", ";
}

this is what I get:

38, 35, 51, 52, 59, 32, 32, 49, 32, 32, 44, 32, 32, 49, 51, 46, 48, 56, 32, 44, 32, 32, 32, 32, 32, 32, 32, 51, 54, 46, 50, 57, 32, 38, 35, 51, 52, 59, //original string
34, 32, 32, 49, 32, 32, 44, 32, 32, 49, 51, 46, 48, 56, 32, 44, 32, 32, 32, 32, 32, 32, 32, 51, 54, 46, 50, 57, 32, 34, 10, 9, //pasted from browser string

What do I have and what can I do about it?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T06:16:23+00:00Added an answer on May 29, 2026 at 6:16 am

    I think I see what’s wrong here. Reversing your output:

    $array1 = array(38, 35, 51, 52, 59, 32, 32, 49, 32, 32, 44, 32, 32, 49, 51, 46, 48, 56, 32, 44, 32, 32, 32, 32, 32, 32, 32, 51, 54, 46, 50, 57, 32, 38, 35, 51, 52, 59);
    $array2 = array(34, 32, 32, 49, 32, 32, 44, 32, 32, 49, 51, 46, 48, 56, 32, 44, 32, 32, 32, 32, 32, 32, 32, 51, 54, 46, 50, 57, 32, 34, 10, 9);
    foreach($array1 as $char){
        echo chr($char);
    }
    echo PHP_EOL;
    foreach($array2 as $char){
        echo chr($char);
    }
    

    we get:

    &#34;  1  ,  13.08 ,       36.29 &#34;
    "  1  ,  13.08 ,       36.29 "
    

    So clearly the issue is the encoding of double-quotes (hence the string(38) length when we expect string(30)). This stems from you filter_var() call. The FILTER_SANITIZE_STRING filter will encode quotes. If you want to stop this from happening, you need to add the FILTER_FLAG_NO_ENCODE_QUOTES flag to your options list. This should prevent the encoding of quotes and leave you with the expected string:

    $options=array(FILTER_FLAG_NO_ENCODE_QUOTES,FILTER_FLAG_STRIP_HIGH, FILTER_FLAG_STRIP_LOW);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a French site that I want to parse, but am running into
I have a reasonable size flat file database of text documents mostly saved in
I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
I need a function that will clean a strings' special characters. I do NOT
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I have a bunch of posts stored in text files formatted in yaml/textile (from
link Im having trouble converting the html entites into html characters, (&# 8217;) i

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.