note 77828 added to function.str-replace
| From: | japxrf at mizzou dot edu | Date: | Fri, 14 Sep 2007 16:34:10 +0000 |
| Subject: | note 77828 added to function.str-replace | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-130495@lists.php.net to get a copy of this message | ||
I thought this might be handy for someone.
I'm working with a CSV file and noticed that excel tends to put extra quotes around portions of
lines that are misformatted. I needed a way to get the section of a line that is surrounded by
quotes, remove the quotes and the commas inside those quotes, then put it back into the line.
Don't knock the code - I'm a novice :)
//assuming here that your current line from CSV file is $data2
if(stristr($data2, '"')){ //if there's a quote in this line it needs fixin'
$quotepos = strpos($data2, '"');
$quotepos_end = strrpos($data2, '"');
$str_len = $quotepos_end - $quotepos;
$badquote = substr($data2, $quotepos, $str_len+1);
$badquote2 = str_replace('"',"",$badquote);
$badquote3 = str_replace(",","",$badquote2);
$data2 = str_replace($badquote, $badquote3, $data2);
}
----
Server IP: 69.147.83.197
Probable Submitter: 67.130.255.251
----
Manual Page -- http://www.php.net/manual/en/function.str-replace.php
Edit -- https://master.php.net/note/edit/77828
Del: integrated -- https://master.php.net/note/delete/77828/integrated
Del: useless -- https://master.php.net/note/delete/77828/useless
Del: bad code -- https://master.php.net/note/delete/77828/bad+code
Del: spam -- https://master.php.net/note/delete/77828/spam
Del: non-english -- https://master.php.net/note/delete/77828/non-english
Del: in docs -- https://master.php.net/note/delete/77828/in+docs
Del: other reasons-- https://master.php.net/note/delete/77828
Reject -- https://master.php.net/note/reject/77828
Search -- https://master.php.net/manage/user-notes.php