Replace spaces with commas from one file and send the output to another file.
tr -s '[:blank:]' ', ' < input.txt > output.txt |
It’s perfect for creating a comma delimited file for importing to Excel.
cloud engineer
Replace spaces with commas from one file and send the output to another file.
tr -s '[:blank:]' ', ' < input.txt > output.txt |
tr -s '[:blank:]' ', ' < input.txt > output.txt
It’s perfect for creating a comma delimited file for importing to Excel.
When performing a database query, we normally use a loop to output the database result. This is a common exercise for programmers when displaying results to the screen. To make the output readable, commas are typically used to separate data. Consider a loop below that prints a list of names. We added a string ‘, ‘ after each name to make it readable.
foreach($query as $item) : echo $item->name.', '; endforeach; |
foreach($query as $item) : echo $item->name.', '; endforeach;
The output would be something like this:
John, Mary, Steve, Mark,
The output works as intended, but there is a trailing comma at the end that we don’t want. We either need to get rid of it, or replace it with a period. So, how do we do that? Here’s how.
We will use the trim() function to remove the last comma. But before we can do that, we should probably hold off on echoing to the screen until the loop is finished. So, we will store the results in a variable called $result as we go through the loop. To avoid a PHP undefined variable error, we need to initialize the $result variable before the loop begins. After the loop, we will use the rtrim function to remove the last comma. Finally, we echo the trimmed results and add a period.
$result=""; foreach($query as $item) : $result.=$item->name.', '; endforeach; $trimmed=rtrim($result, ', '); echo $trimmed; echo '.'; |
$result=""; foreach($query as $item) : $result.=$item->name.', '; endforeach; $trimmed=rtrim($result, ', '); echo $trimmed; echo '.';
The output is now:
John, Mary, Steve, Mark.
This article will show you how to remove commas, semicolons, or any character, from a form. This tidy PHP script is quite useful when filtering out unwanted characters submitted in a form. This is important when the data is collected and exported later to a CSV (comma-delimited values) file. CSV files use commas and semicolons to separate data values. Having commas and semicolons in your data creates confusion and unwanted results. This neat PHP script removes commas and semicolons, and replaces them with a blank value.
First, the HTML form. This is just a HTML form with one field and a submit button. The form calls itself when submitted since the “action” field is left blank.
<form method="post" action=""> <input name="field"/> <input name="submit" type="submit"/> </form> |
<form method="post" action=""> <input name="field"/> <input name="submit" type="submit"/> </form>
Now, the fun part. I came up with a list of characters that I want filtered out. In this example, I want to filter out commas and semicolons only. The unwanted characters are placed in an array. I cleaned up the input data using a PHP function called str_replace(). This PHP function replaces commas and semicolons with blanks. Finally, the results are assigned to a variable called $clean and echoed to the screen.
$list = array(",", ";"); $clean = str_replace($list, "", $_POST['field']); echo $clean; |
$list = array(",", ";"); $clean = str_replace($list, "", $_POST['field']); echo $clean;
Taking one more step. I made the code modular by creating a function called clean_input(). I can then invoke the function numerous times on my script without repeating code. I simply call the clean_input() function when I need it. The function uses the same code above with some slight modification. We are passing the $in variable to the function. This is going to be in input field on our form. The clean_input function replaces it, and the results are returned.
function clean_input($in) { $list = array(",", ";"); $clean = str_replace($list, "", $in); return $clean; } $output = clean_input($_POST['field']); echo $output; |
function clean_input($in) { $list = array(",", ";"); $clean = str_replace($list, "", $in); return $clean; } $output = clean_input($_POST['field']); echo $output;
Finally, we can put all the pieces together in a single file called form.php. The input value in the form will be echoed after submission. Commas and semicolons are filtered out by our PHP function. We can test the function in the demo below. Just type any value to our input field. If you type a comma or semicolon, the input is replaced with a blank.
<form method="post" action=""> <input name="field"/> <input name="submit" type="submit"/> </form> <?php function clean_input($in) { $list = array(",", ";"); $clean = str_replace($list, "", $in); return $clean; } $output = clean_input($_POST['field']); echo $output; /* end of file */ |
<form method="post" action=""> <input name="field"/> <input name="submit" type="submit"/> </form> <?php function clean_input($in) { $list = array(",", ";"); $clean = str_replace($list, "", $in); return $clean; } $output = clean_input($_POST['field']); echo $output; /* end of file */