Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding a \n newline symbol when using sprintf in PHP

I'm trying to export existing data from a MySQL database, into ruby commands I can run with rake db:seed.

Here's my code.

# Generate db:seed data for proveedores.
$proveedores = R::findAll('tbproveedores');
$proveedoresE = R::exportAll($proveedores);

foreach ($proveedoresE as &$p) {

    $line = 'BookSupplier.create(company: "%s", city: "%s", country: "%s", address: "%s", telephone: "%s", contact: "%s", email: "%s", website: "%s"\n';
    $exportedLine = sprintf($line, $p['empresa'], $p['ciudad'], $p['pais'], $p['direccion'], $p['telefono'], $p['personacontacto'], $p['email'], $p['website']);
    var_dump($exportedLine);
    fwrite($seeds, $exportedLine);
    echo "<br />";
}

Notice the \n newline symbol at the end of the $line variable. I read online that that was all that is needed to use a newline.

The output of my code above is (vertabim, a long line):

BookSupplier.create(company: "Pearson", city: "Lima", country: "Peru", address: "Av. Limon", telephone: "4673535421", contact: "Javier", email: "", website: ""\nBookSupplier.create(company: "Project Management Institute - PMI", city: "Pennsylvania", country: "Estados Unidos", address: "Newtown Square, Pennsylvania", telephone: "1", contact: "Limberg Morales", email: "", website: "http://www.pmi.org/"\nBookSupplier.create(company: "UVirtual - Centro de Excelencia", city: "Santa Cruz", country: "Bolivia", address: "Av. Irala 585", telephone: "1", contact: "Limberg Morales", email: "", website: ""\nBookSupplier.create(company: "Ábaco de Rodolfo Depalma", city: "Buenos Aires", country: "Argentina", address: "Viamonte 1336, 4° (C1053 ACB) Buenos Aires", telephone: "5411-43711675", contact: "Limberg Morales", email: "", website: "http://www.abacoeditorial.com.ar/"\nBookSupplier.create(company: "Pablo Lledó - ProjectManagement", city: "Canadá", country: "Estados Unidos", address: "Victoria, BC, Canadá", telephone: "1", contact: "Limberg Morales", email: "", website: ""\n

I'd like each call to be in it's own line, following my newline insertion. Any suggestions on what I'm doing wrong?

like image 626
sergserg Avatar asked Feb 26 '13 14:02

sergserg


People also ask

How do I add a new line to a string in PHP?

Using new line tags: Newline characters \n or \r\n can be used to create a new line inside the source code.

What is the \n in PHP?

\n is the newline or linefeed, other side \r is the carriage return. They differ in what uses them. Windows uses \r\n to signify the enter key was pressed, while Linux and Unix use \n to signify that the enter key was pressed.

What does sprintf () function do in PHP?

The sprintf() is an in-built function of PHP which writes a formatted string to a variable. It returns a formatted string.


1 Answers

If you can't (or don't want to) switch to double quotes, you can insert a newline using the ASCII code like so:

> php
<?php printf('line one%cline two', 10);^D
line one
line two
>

The %c lets you insert any ASCII code and 10 is the code for a newline.

like image 93
Josh Avatar answered Sep 25 '22 02:09

Josh