Tracking Forums, Newsgroups, Maling Lists
Home Scripts Tutorials Tracker Forums
 
  HOME    TRACKER    PHP




Removing A Comma From The End Of An Array


I am reading a file into an array using this:

$listFile = "add2db.txt";
if (!($fp = fopen($listFile, "r")))
exit("Unable to open the input file, $listFile.");
$buffer = fread($fp, filesize($listFile));
fclose($fp);

The very last character in that $listFile will always be a comma. What I need to do is remove that last comma (and only that comma) from $buffer before I can continue on. What do I need to do to accomplish this?




View Complete Forum Thread with Replies

See Related Forum Messages: Follow the Links Below to View Complete Thread
Seperating An Array With A Comma
I'm trying to seperate the output of an array with a comma. I can't figure out how to eliminate the last comma. Here is my code:

$result = mysql_query($query,$connection) or die (mysql_error());
$fw = fopen("/var/www/html/ecast/windex/graph.html","w");
fwrite($fw, "/ ");
if ($new_rows = mysql_fetch_array($result)){
do{
fwrite($fw, $new_rows[0]);
fwrite($fw, ", ");
}while($new_rows = mysql_fetch_array($result));
}
fwrite($fw, " /");
fclose($fw);
?>
Output: / 85, 86, 87, 89, /
How can I eliminate that last comma?

Exploding Comma Separated In Array
I'm looking to explode the comma separated values in an array like this:

Array ( => 10811 [1] => 7527,7556 )

and then add the exploded value as a new key in the array...like this:

Array ( => 10811 [1] => 7527 [2] => 7556 )

Looping Thru Array And Creating Comma Sep String
I'm trying to loop through the elements of an array and create a comma separated string of the values, and then trim off the last comma. Here's the code I have which only displays the last element of the array in the string: PHP Code:

Removing Parts Of An Array
how can i remove parts of the array with no 'picinfo'? Code:

Removing Element From Array Using String
I am trying the following based on an example from php.net

<?php

$array_of_items = array ('A','B','C','D');
array_splice($array_of_items, 'B', 1);

foreach ($array_of_items as $array_of_items2)
{
Print $array_of_items2;}?>

I expect the result to be ACD, I am getting BCD, Not sure why.

Removing A Single Value From $_SESSION Array.
What I have here is three select boxes one for all the tables in the db, one listing the table columns in that table and a third to collect the table columns selected stored in a session called $_SESSION['collections'].  Up to this point all that works beautiful the session stores the selected collections.

But heres what I want to be able to do: I want to remove single values from the $_SESSION['collections'].  How do I do that? Code:

Removing An Element From The POST Array
I have a form that is posting it data to PHP_SELF. This then gets processed and does it's thing. My question is that when I hit the submit button, I get all the data from my form but I also get one item from the Submit button itself. Can I make the submit button not include itself in the POST?

Here is an example output of print_r($_POST)
Array
(
    [ACPlatSearch] => -1
    [ACDeedSearch] => -1
    [ACUserInfo] => -1
    [ACPropertySearch] => -1
    [ACStatsTotalSales] => -1
    [SavePermissions] => Save
)

The last element is the submit button, the other ones are checkboxes.  I don't want that last one.  So is there a simple way to not include it or do I need to loop through the array and remove it manually?  I need the rest of the array intact for further processing with array functions.

Need Help Putting Data Into Array / Removing Duplicates
I've got a variable that looks like:

31, 1, 1,1,1,1,164, 164,29, 29,29,29,37,2,2,2,2,2,2,
147,147,26,26,49, 138, 138,138,138,138,138,138,137, 137,168,
170, 170,48, 172, 44, 187, 183, 185, 195, 184, 186, 45, 46,
194, 167, 182, 181, 34, 18, 16, 25, 28, 53, 53,11, 11,10, 20,
192, 39, 54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,
54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,
54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,
54,54,54,54,54,58, 58,58,58,58,58,58,58,58,58,58,58,58,58,58,
58,58, 175, 176, 180, 177, 178, 179,


I'm sending it to a mysql query which will say NOT IN($variable 0) (the 0 is added because the variable ends with a comma). I'd like to remove the duplicates but have no idea how. I figured if I stick it in an array it will be easier. There is an array_unique function after all. But I have no idea how to get the data in an array. I tried $arr1 = str_split($variable); but it spit out an error, probably due to the trailing comma.

Removing Item From Array Then Overwriting File
I have a bookings.txt file which has a list of dates each on a new line, for example: -

21/06/07
22/06/07
29/06/07
25/12/07
01/01/08

I also have a variable $event_dd_mm_yy which I want to remove from this file if found. Code:

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_addresses = count($addresses);
if ($number_of_addresses == 0)
{
echo "No addresses to process";
}

echo "<table>";

for($i=1; $i<$number_of_addresses; $i++)
{
//split each line or row
$line = explode(" ",$addresses[$i]);

Comma Seperator
I have numbers retrieved from database which is quite big. Is it possible to give a comma seperator in the sql query or otherwise
how can I format with a thousand comma separator?

Deleting Last Comma
The code below displays pet names like so:

Fido, Bowser, Kittycat, Spot,

and I want to delete only the last comma. I tried 'substr' and 'rtrim' and both of those delete the space and comma between each name, not just the last name as I'd like, so I end up with:
Fido,Bowser,Kittycat,Spot,

How would I delete only the last comma, so that it is this instead?
Fido, Bowser, Kittycat, Spot

Even with trim and substr, I end up with:

Comma Separated Values
What i've done is when the user enters the client list separated by commas..... this works, but I need to know how to warn the user when they have enter the client list with no commas.

Is there some sort of function which does this or is it a regular expression thing??

Convert Number To One With A Comma
How can I convert "2000" to "2,000?"

Comma Delimited File
I am trying to read comma delimited rows of text. The problem is that
some fields may be encapsulated in "" - particularly the text fields but
not numeric fields.

Is there a simple efficient way to parse the fields with comma but also
strip off the "" encapsulating some of the fields with php?

The problem is the "" encapsulation is optional. Some fields will have
it, some won't.

How To Instert Comma Into Number?
I am having users submit large numbers into a form, and when those values are returned to them later I want to have the commas in the correct places. ie. 123456.00 changes to 123,456.00. Is there a function that does this conversion? Also, how would I ensure that the number is always displayed with 2 decimal places, ie. 123 changes to 123.00, but 123.00 remains 123.00.

Strip Comma From Number
i have a number 1,553.00 i need to remove the comma and store the value? any ideas?

Re Comma Separated List - Another Question
I can do the match perfectly but what i also need to do is create a third
list of comma separated values that are in both

eg:

List 1 => 1,2,3,4,5,6,7,8,11
List 2 => 1,3,4,5,6,7,10,23

Therefore

List 3 => 1,3,4,5,6,7

How do I populate this third list - I'm really stuck ....

Breaking Up A Comma Seperated List
i have a list seperated by commas: 1. 3. 5. 7. 9 actually is a varible

$members ="1, 3, 5, 7, 9"; what would be the best way to break that up to insert the values into a table in a loop.

Comma Seperated List Comparison
is it possible to compare acomma separated list aginst another

eg comma list 1 => 1,2,3,4,5
comma list 2 => 3,5
can you check that 3 is in both, and 5 is in both, therfore they match?

the comparison is to check that if product a who supplies products 1,2,3,4,5
can be used instead of product b who supplies 3,5 as product a already
supplies them

Seperator Instead Of A Comma For Listed Items In DB?
I'm making a recipe database, and need to have DB fields in mySQL that
will have lists of values that would get sent to an array to be worked
on.

I anticipate that at times a comma will need to be used in the value
itself. So, what's a commonly used symbol or something that people tend
to use as a separator of a list that would then be used in the split()
function to send the elements into an array?

How To Check If It's The Last Result And Remove Comma
I am wondering if anyone could help. I have a loop that displays my results and places a comma after each. The problem is that when the last result is displayed, it shows the comma.

Example:
for loop{
echo $row['test'].', ';

}

displays...  test1, test2, test3,

Notice after test3, there is still a comma? How do I remove it and have it display... test1, test2, test3

Comma Separated Text Files
A client has requested that I make a php app which takes an uploaded image, reads the values and enters them into a database. I've made the upload script which works fine, and the script which enters the values into the database, the only thing left now is how do I make PHP read the file and extract the values? The file is a text file, each field comma separated.

How To Remove Last Comma From A Multiple List?
I have a multiple select list in my site where the values are inserted in a database table, in a column named available_country. I enter these values in the db separated with comma. I don't know however how to remove the last comma so it will be something like this:

USA, Europe, Asia

and not:

USA, Europe, Asia,

Here's what I tried but it didn't worked: ...

Comma Delimited Csv Has Commas Inside Fields
I need to import a CSV file into a database daily. That operation in itself is simple enough but the trouble is that some fields are quoted because they have commas inside them. How do I handle this with my import script?

It would be much better if the CSV was created with all fields quoted or used a different delimited but sadly that is not an option.

Regular Expression For Comma-delimited Pairs
I have a textarea form field for inputting (or pasting) pairs of data.

I need a regular expression pattern to validate each line for the following

double quote
number
double quote
comma
double quote
alpha string
double quote
carriage return

The following comes close, but doesn't check for a carriage return at the
end of each line:

^"([0-9]?)+"([,]s?"([A-Za-z0-9]+)")*$

For example the following would return true:

"1","John"
"2","Paul"
"3","George"
"4","Ringo"

Reading Excel To MySql Or Comma Delimited ...
right direction on how to deal with an xls
flie.

but the only other file I have to work
with is pdf.

I'm not sure, is xls the better of two evils?


Stripping Quotes Out Of A Comma Delimited File?
I have a flat file that I'm trying to stick into a MySQL database. One
record per line, multiple fields per record, and many of them are null
fields which are just double quotes without a space between. It's probably
nothing really major for people who have done this before, but I'm a bit
stumped. The file is comma delimited. Every field is surrounded by double
quotes. I've done quite a bit of searching, on the php site and elsewhere,
but I can't seem to get it to strip the quotes out so I can explode the file
line by line to grab the fields.

Comma Separated List In MySQL Field
say I have a list of categories in a field, and a page that is supposed to pull from the db based on a category. Is there any way I can do a query where it searches the contents of that field for the category in question?

like SELECT FROM table WHERE category = $category  but have it search the category field (the comma separated list for each entry) for the $category var

Seperate Links From Comma Seperated Value In A Databse
I have a value which is $keywords in my database The key words are separated by commas, like:  fun, funny, exciting, weird I want to make each of those keywords link to "link.php" I can only get them all to be one link, how would I make them each into seperate links.

Easy Comma-delimited List To Dropdown Box Converter
I was working on a simple converter when I realized that by not setting the id param of the option tag, it wasn't going to send the value of the dropdown box to my php script. I had this to start with: Code:

How To Remove "comma, Full Stop" From A Sentence
I have sentences as follow:

$quote[1] = "To be or not to be, that is the question.";
$quote[2] = "Love looks not with the eyes, but with the mind.";
$quote[3] = "Love all, trust a few. Do wrong to none.";

now I would like to do the following,

1) for each quote[$i], remove all ","(comma) or "."(full stop)...does anyone know how? I have the following code but it does not work. What should I write in $patterns?

for ($i=1; $i<4; $i++)
{
$quote[$i] = strtolower($quote[$i]);
echo "$quote[$i]<BR>";
$pieces = explode(" ", $quote[$i]);
for ($j=0; $j< (count($pieces)); $j++)
{
echo "$pieces[$j] ";
$patterns = ".";
$replacement = "";
print preg_replace($patterns, $replacement, $string);
echo "$pieces[$j] <BR>";
}
}

2) also, I would like to count a) number of frequecy of each term (e.g. "to" has a frequency of 2 in qoute[1]) and b) number of frequency for all three quotes (i.e. "to" has frequency = 3)

but I have no idea how? can anyone please give me an idea?

Removing Bad Words
Looking for suggestions on how to handle bad words that might
get passed in through $_GET['item'] variables.

My first thoughts included using str_replace() to strip out such
content, but then one ends up looking for characters that wrap
around the stripped characters and it ends up as a recursive
ordeal that fails to identify a poorly constructed $_GET['item']
variable (when someone hand-types the item into the line and
makes a simple typing error).

So the next thoughts involved employing a list of good words
and if any word in the $_GET['item'] list doesn't fall into the
list of good words, then an empty string gets returned.

Any suggestions on how to handle this?

Removing PHP Coding
I have php coding that is stored in mysql blobs. Now I want to index words in these blobs in an attempt to make some sort of a search engine. Example: How can I use php to make this mysql text:

"Once upon a time there were <?php echo $num_bears ?> bears in a small cage in a zoo in Russia. They had <?php echo $num_relatives ?> relatives that lived there with them."

Change into this: "Once upon a time there were bears in a small cage in a zoo in Russia. They had relatives that lived there with them."

Point is: - remove everything between and including "<?php" and "?>" I'm trying to do some indexing, and I need to remove all my php coding... :)

Removing Characters
I am passing a string over to a calculator form but need to strip out the £ and ,
the string comes over as /index.php?price1=£195,000 this populates an input field as below
<input type="text" name="price1" class="fields" value="<? if ($price1 != "") { echo $price1; } else { echo "100000"; }?>"> I have added at the top of the page

<?
$price1 = str_replace("£", "", $price1);
$price1 = str_replace(",", "", $price1);?>

this strips the £ but gives an error with the comma.

Removing Whitespaces
I try to remove whitespaces from a string using the preg_replace function
like

preg_replace("/[ ][ ]*/"," ",$string);

This works great, but makes no handles all characters in the same way. I
want to preserve data between double quotes. So,
hello world "It is a sunny... world"
shall be reduced to
hello world "It is a sunny... world"

and should not remove the whitespaces between the quotes.

Removing File
I created a dir with a file in it, using php. I set the file mode to 'w'. It worked and everything, but now i can't delete the file. I tried deleting it using php, in ftp, and in ssh. I can't set chmod mode either. I tried chown but that doesn't work. For anything you do, it shows "Operation not permitted".

Removing The .php From Pages
Hi i am new here we run a website called www.example.com and i was wondering if it is possible to remove the .php from the end of a address

For example www.example.com/newsinbrief.php, it would be nice it I could have it as www.example.com/newsinbrief

Is this possible does anyone know how to do it and if so where i could look for the code advise etc.

Removing Duplicates
I'm getting some data from a database, but there are duplicates for every entry. For instance, if you looked a table of posts for this forum, you'd see posts and the author.  I'm getting the author, so there could be hundreds or more of repeats. I'd like to basically get all the uniques in a column. 

Removing Value From Echo
For example.  If had the following coordinates in the database:

-5.717979 54.728432,-5.672246 54.757900,-5.621388 54.786976,

If I then echo them how would I remove the , (comma) at the end?  As I will echoing multiple coordinates from the database, some longer than others I can't simply remove the comma after a certin about numbers.

Removing Delimiter?
I'm trying to remove the delimiter when I use my functions and replace it like PHPBB does {CODE}  instead of <?CODE()?>

What would be a easy way to do that?

Removing Non Characters......
I wanna remove everything else but a-z in my source file ........and replace everything that is not a-z with nothing. I have the below example (but it's getting a parse error). Actually is there an easier way to remove noncharacters (e.g. !@$*$%) from my source file.

<?
$q = $stringoftext
$search = array (""'#'","'$'","'%'","'&'","''","'/'","'@'","'!'","'('","')'","'['","']'","'.'","'+'","'-'","'*'","'{'","'}'","'?'"");
$replace = array ("");
$body = preg_replace ($search, $replace, $q);
?>

Removing Block Of Text
Is there anyway with PHP you could remove a whole block of text???

Say if I had three lines of text and I wanted to strip those three lines out, but the only problem is, it might appear at the top or bottom of the page...How would I do this??

Removing Img Tags With Eregi_replace
I'm trying to remove all image tags from a html document.

<H1>header</H1>
<P>foo<IMG src="asdf.jpg">
<BR>
foo...</P>

I want the "<IMG src="asdf.jpg">" to go away... (for display on an AvantoGo channel).

I suck at regexs...

$html = eregi_replace("<img.*.>", "", $html);

Nukes everything after the img tag (and the image tag)...

Removing A Space After Submitting
I am working on a script that creates options with a number to be inserted into an array PHP Code:

Removing Html From A Blob Via Php
I'm trying to use php ereg coding to remove all html from a blob so that I can parse it into keywords, any Ideas?

Removing Every Even Character From A String
Say I had .a.b.c and wanted to return abc. Any ideas as to how I'd do
this? Here's my attempt, but it doesn't work:
<?php
echo preg_replace('#(?:.(.))+#s', '$1', '.a.b.c');
?>

Help Removing A Simple Space
I need to check for sentences that have a space before the period and
remove the space. I've tried:

$sentence = str_replace(" .", ".", $sentence);

but it does not work. I've tried other ways with no success.

Why doesn't this work and how do I do this?

Removing A Line Within A File?
I have an include file (links.php) with nothing but links, the filenames are dates ( i.e. YYYYMMDD_hhmmss.php), all the links point to files within the same directory (i.e. /data). I read all these files in from the directory with readdir() and then build a form in order to remove certain files. I need to be able look into the include file, find the correct link, and remove it. I have been trying to use a foreach statement and preg_match() and printing back to the file lines that do not match and skipping the ones that do with absolutely no luck. It doesn't seem to match the link within the file.
Here is a snippet of code:


Copyright © 2005-08 www.BigResource.com, All rights reserved