Removing quotes from an array, string or txt file see code

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Techie Guy

    Removing quotes from an array, string or txt file see code

    I'm trying to process a tab delimited file where each line in the file
    has around 12 tab delimited elements. My problem is the elements are
    surrounded by "quotes" and I need the script to remove the quotes
    after loading the file to the $addresses variable.
    The quotes need to be removed somewhere within the code below or by
    processing the file before loading.

    //read in the name and address lines from the txt file
    //each line becomes an element in the array
    $addresses= file("download. txt");

    //count the number of address lines in the array
    $number_of_addr esses = count($addresse s);
    if ($number_of_add resses == 0)
    {
    echo "No addresses to process";
    }

    echo "<table>";

    for($i=1; $i<$number_of_a ddresses; $i++)
    {
    //split each line or row
    $line = explode("\t",$a ddresses[$i]);

    TIA

    More info if required

  • Joe Estock

    #2
    Re: Removing quotes from an array, string or txt file see code

    Techie Guy wrote:[color=blue]
    > I'm trying to process a tab delimited file where each line in the file
    > has around 12 tab delimited elements. My problem is the elements are
    > surrounded by "quotes" and I need the script to remove the quotes
    > after loading the file to the $addresses variable.
    > The quotes need to be removed somewhere within the code below or by
    > processing the file before loading.[/color]

    Simple.
    [color=blue]
    >
    > //read in the name and address lines from the txt file
    > //each line becomes an element in the array
    > $addresses= file("download. txt");[/color]

    This is a rather barbaric way to read in a file. What happens if the
    file is larger than the memory PHP is allowed to use? You should
    concider reading the file in line by line instead with the following:

    $fp = fopen('download .txt', 'r');

    while(!feof($fp ))
    {
    $line = fgets($fp);
    /* do stuff */
    }

    fclose($fp);
    [color=blue]
    >
    > //count the number of address lines in the array
    > $number_of_addr esses = count($addresse s);
    > if ($number_of_add resses == 0)
    > {
    > echo "No addresses to process";
    > }
    >
    > echo "<table>";
    >
    > for($i=1; $i<$number_of_a ddresses; $i++)
    > {
    > //split each line or row[/color]

    Before you split the line using explode, simply do:

    $line = explode("\t", str_replace('"' , '', $addresses[$i]));
    [color=blue]
    > $line = explode("\t",$a ddresses[$i]);
    >
    > TIA
    >
    > More info if required
    >[/color]

    TIA? IMHO this is bad netiquette - if you can't be bothered to take the
    time to write out "Thanks in advance" why should we be bothered to help?

    Cheers,

    Joe

    Comment

    • Techie Guy

      #3
      Re: Removing quotes from an array, string or txt file see code

      On Sun, 30 Oct 2005 15:01:07 GMT, Joe Estock
      <jestock@NOSPAM nutextonline.co m> wrote:
      [color=blue]
      >Techie Guy wrote:[color=green]
      >> I'm trying to process a tab delimited file where each line in the file
      >> has around 12 tab delimited elements. My problem is the elements are
      >> surrounded by "quotes" and I need the script to remove the quotes
      >> after loading the file to the $addresses variable.
      >> The quotes need to be removed somewhere within the code below or by
      >> processing the file before loading.[/color]
      >
      >Simple.
      >[color=green]
      >>
      >> //read in the name and address lines from the txt file
      >> //each line becomes an element in the array
      >> $addresses= file("download. txt");[/color]
      >
      >This is a rather barbaric way to read in a file. What happens if the
      >file is larger than the memory PHP is allowed to use? You should
      >concider reading the file in line by line instead with the following:
      >
      >$fp = fopen('download .txt', 'r');
      >
      >while(!feof($f p))
      >{
      > $line = fgets($fp);
      > /* do stuff */
      >}
      >
      >fclose($fp);
      >[/color]

      Point taken the file rarely has more than 20 lines but I will use the
      code supplied because I think it is an improvement.
      [color=blue][color=green]
      >>
      >> //count the number of address lines in the array
      >> $number_of_addr esses = count($addresse s);
      >> if ($number_of_add resses == 0)
      >> {
      >> echo "No addresses to process";
      >> }
      >>
      >> echo "<table>";
      >>
      >> for($i=1; $i<$number_of_a ddresses; $i++)
      >> {
      >> //split each line or row[/color]
      >
      >Before you split the line using explode, simply do:
      >
      >$line = explode("\t", str_replace('"' , '', $addresses[$i]));
      >[/color]

      I had thought of the str_replce function but couldn't think how to use
      it... thank you.
      [color=blue][color=green]
      >> $line = explode("\t",$a ddresses[$i]);
      >>
      >> TIA
      >>
      >> More info if required
      >>[/color]
      >
      >TIA? IMHO this is bad netiquette - if you can't be bothered to take the
      >time to write out "Thanks in advance" why should we be bothered to help?[/color]

      Assuming that the reason for using TIA is "can't be bothered to take
      the time to write it out", then I agree it could be poor netiquette.
      However if there is another reason for it's use then it's not poor
      netiquette.
      [color=blue]
      >
      >Cheers,
      >
      > Joe[/color]

      Cheers and thanks again

      Comment

      Working...