1

this is my txt file

I'm adding data from .txt to database with this code:

$dosya=new SplFileObject('veriler.txt');
while(!$dosya->eof())
{
    $satir=$dosya ->fgets();
    list($name,$section,$initialname)=explode(' ',$satir);
     
   $sth= $baglan->prepare('INSERT INTO tablo1 values (NULL,?,?,?,NULL)');
    $sth->bindValue(1,$name,PDO::PARAM_STR);
    $sth->bindValue(2,$section,PDO::PARAM_INT);
    $sth->bindValue(3,$initialname,PDO::PARAM_STR);
    $sth->execute();
     
}

In the .txt if there is a 1 space between the words, my program is working. But as you can see, there are more than one space in my txt file. How can i delete/remove multiple spaces in .txt file? If you can show me in my codes, i will be glad. Thank you.

Erdem C.
  • 9
  • 3

3 Answers3

1

You can use a regular expression as well to archive the same result.

<?php
// Your code here!
$string = "This  has    too  many spaces";
$result = preg_replace("/\s{2,}/", ' ', $string);
echo($result);
?>

Where /\s{2,}/ means after 2 spaces replace it with a single space also consider that \s also means any of the following characters:

  • \r
  • \n
  • \t
  • \f
  • \v
  • (empty space)

Link: https://paiza.io/projects/M6eSG1zHIUdG5IZEXFZQog

\s stands for “whitespace character”. Again, which characters this actually includes, depends on the regex flavor. In all flavors discussed in this tutorial, it includes [ \t\r\n\f]. That is: \s matches a space, a tab, a carriage return, a line feed, or a form feed.

You can read more about this over here: https://www.regular-expressions.info/shorthand.html

Crisoforo Gaspar
  • 3,740
  • 2
  • 21
  • 27
  • 1
    Might want to use `\h` instead to match only for horizontal whitespaces. Depends on what he wants to do with newlines, though. Also a small nitpick, according to regular-expressions.info, `\s` doesn't match the entirety of `\v`, but only `\r`, `\n` and `\f`(form feed character). – Jeto Apr 17 '20 at 15:29
  • Thanks I think that's a great suggestion. – Crisoforo Gaspar Apr 18 '20 at 13:39
0

explode() the string, remove array elements with whitespace, and implode():

<?php
    $string = "This  has    too  many spaces";
    $array = explode(" ", $string);
    $array = array_filter($array); 
    $result = implode(" ", $array);
    echo($result);
?>

https://paiza.io/projects/Bi-2H7HiPIklLwXGfYAqCg

symlink
  • 11,984
  • 7
  • 29
  • 50
  • So in OPs case this would be `list($name,$section,$initialname)=array_filter(explode(' ',$satir));`? – Nigel Ren Apr 17 '20 at 15:19
  • They're better off splitting it into a few lines. – symlink Apr 17 '20 at 15:21
  • Thanks for your answer, in the string it's working, but i want to replace my .txt file and all of words should be 1 space. I am an amateur in the php, sorry for my easy question – Erdem C. Apr 17 '20 at 15:23
0

@Crisoforo Gaspar solution in your code :

    $dosya=new SplFileObject('veriler.txt');
while(!$dosya->eof())
{
    $satir=$dosya ->fgets();
    $satirWithoutManySpaces = preg_replace("/\s{2,}/", ' ', $satir);
    list($name,$section,$initialname)=explode(' ',$satirWithoutManySpaces);

   $sth= $baglan->prepare('INSERT INTO tablo1 values (NULL,?,?,?,NULL)');
    $sth->bindValue(1,$name,PDO::PARAM_STR);
    $sth->bindValue(2,$section,PDO::PARAM_INT);
    $sth->bindValue(3,$initialname,PDO::PARAM_STR);
    $sth->execute();

}

Hope this help

threeside
  • 323
  • 2
  • 10
  • it's not working, my .txt file is: https://prnt.sc/s1ag10 , when i write your codes, it is added database like this: https://prnt.sc/s1ael5 , – Erdem C. Apr 17 '20 at 15:38
  • You're right, i make a mistake. I think you should use @Crisoforo solution on your $satir var, and after you can use explode function. This should work as expected. I edit my answer – threeside Apr 17 '20 at 16:04