Jump to content

substr_replace help


dennismonsewicz

Recommended Posts

$row = 1;
$handle = fopen("test.csv", "r");
	while (($data = fgetcsv($handle, ";")) !== FALSE) {
		$num = count($data);
				    echo "<p> $num fields in line $row: <br /></p>\n";
				    $row++;
				    for ($c=0; $c < $num; $c++) {
				        echo substr_replace($data[$c], ",", -1);
				    }
				}
				fclose($handle);

 

The code above takes a csv file and dumps the results to the screen... I am wanting to strip the final comma off of the result:

 

normal output:

INSERT INTO `tblName` VALUES(1,'att','AT&T ','5700','50',' ',' ',' ');,

 

what my code outputs:

INSERT INTO `portal_deductible` VALUES(,'att,'AT&T ,'5700,'50,' ,' ,' '),

Link to comment
Share on other sites

see if this function works for you

function removeLast($string, $char){
$pos = strrpos($string, $char);
$newString = substr_replace($string, '', $pos, strlen($string));
return $newString;
}

 

with the following code

$string = "INSERT INTO `tblName` VALUES(1,'att','AT&T ','5700','50',' ',' ',' ');,";
echo removeLast($string, ',');

 

the output is

INSERT INTO `tblName` VALUES(1,'att','AT&T ','5700','50',' ',' ',' ');

Link to comment
Share on other sites

here is my code

 

function removeLast($string, $char){
					$pos = strrpos($string, $char);
					$newString = substr_replace($string, '', $pos, strlen($string));
					return $newString;
				}

				$row = 1;
				$handle = fopen("test.csv", "r");
				while (($data = fgetcsv($handle, ";")) !== FALSE) {
				    $num = count($data);
				    echo "<p> $num fields in line $row: <br /></p>\n";
				    $row++;
				    for ($c=0; $c < $num; $c++) {
						echo removeLast($data[$c], ',');
				    }
				}
				fclose($handle);

 

nothing echoes out on the screen now :(

Link to comment
Share on other sites

@Mark Baker: The CSV file contains the insert into sql script

 

sample of the csv:

INSERT INTO `tblName` VALUES(1, 'att', 'AT&T ', '5700', '50', ' ', ' ', ' ');
INSERT INTO `tblName` VALUES(2, 'att', 'AT&T ', '8525', '125', ' ', ' ', ' ');
INSERT INTO `tblName` VALUES(3, 'att', 'AT&T ', 'TILT (8900) ', '50', ' ', ' ', ' ');
INSERT INTO `tblName` VALUES(4, 'att', 'AT&T ', 'TILT (8925) ', '125', ' ', ' ', ' ');
INSERT INTO `tblName` VALUES(5, 'att', 'AT&T ', 'USBCONNECT 881 ', '50', ' ', ' ', ' ');
INSERT INTO `tblName` VALUES(6, 'att', 'AT&T ', 'USBCONNECT MERCURY ', '50', ' ', ' ', ' ');

Link to comment
Share on other sites

print_r on $data[$c] prints the data out, but for some reason the function does nothing for me :(

 

code:

 

function removeLast($string, $char){
					$pos = strrpos($string, $char);
					$newString = substr_replace($string, '', $pos, strlen($string));
					return $newString;
				}

				$row = 1;
				$handle = fopen("test.csv", "r");
				while (($data = fgetcsv($handle, ";")) !== FALSE) {
				    $num = count($data);
				    echo "<p> $num fields in line $row: <br /></p>\n";
				    $row++;
				    for ($c=0; $c < $num; $c++) {
						//print_r(removeLast($data[$c], ','));
						//print_r($data[$c]);
				    }
				}
				fclose($handle);

Link to comment
Share on other sites

erm It is a bit confusing what you are trying to get at

if your data is delimited by ";"

 

try

$data = fgetcsv($handle, 0 , ";")

 

and none of that substr_replace stuff

 

to get the ";" back

$data[0] = $data[0].";";

 

 

dunno what you are trying to do with

echo "<p> $num fields in line $row: <br /></p>\n";

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.