Rectangle 27 6

1. The enclosure parameter is the character the encapsulates the data for a specific field, or "index". By default, the parameter is a ", which means that you can "enclose" strings in " characters.

1,2,"this is three",4

2. Per a comment, you're calling fgetcsv($handle, 10000, ','). It's possible, maybe, that the line(s) you're reading are longer than 10000 characters. Try changing the length to 0, which will be "no limit" and see if that helps. Another solution would be to try wrapping the column's value in double-quotes.

while (($data = fgetcsv($handle, 10000, ',')) !== FALSE) {
$title = $data[0];

csv - PHP fgetcsv() delimiter - Stack Overflow

php csv fgetcsv readfile
Rectangle 27 0

Returns a value in the range of 1 to N if the string str is in the string list strlist consisting of N substrings. A string list is a string composed of substrings separated by , characters. If the first argument is a constant string and the second is a column of type SET, the FIND_IN_SET() function is optimized to use bit arithmetic. Returns 0 if str is not in strlist or if strlist is the empty string. Returns NULL if either argument is NULL.

WHERE nums LIKE '%21%'

will not work, as that would then also include 211 and 121

php - Query: Find string in database field - Stack Overflow

php mysql database
Rectangle 27 0


You may want to parse the query string into an array. 

<?php
/**
* Similar to parse_str. Returns false if the query string or URL is empty. Because we're not parsing to
* variables but to array key entries, this function will handle ?[]=1&[]=2 "correctly."
*
* @return array Similar to the $_GET formatting that PHP does automagically.
* @param string $url A query string or URL
* @param boolean $qmark Find and strip out everything before the question mark in the string
*/
function parse_query_string($url, $qmark=true)
{
if (
$qmark) {
$pos = strpos($url, "?");
if (
$pos !== false) {
$url = substr($url, $pos + 1);
}
}
if (empty(
$url))
return
false;
$tokens = explode("&", $url);
$urlVars = array();
foreach (
$tokens as $token) {
$value = string_pair($token, "=", "");
if (
preg_match('/^([^\[]*)(\[.*\])$/', $token, $matches)) {
parse_query_string_array($urlVars, $matches[1], $matches[2], $value);
} else {
$urlVars[urldecode($token)] = urldecode($value);
}
}
return
$urlVars;
}

/**
* Utility function for parse_query_string. Given a result array, a starting key, and a set of keys formatted like "[a][b][c]"
* and the final value, updates the result array with the correct PHP array keys.
*
* @return void
* @param array $result A result array to populate from the query string
* @param string $k The starting key to populate in $result
* @param string $arrayKeys The key list to parse in the form "[][a][what%20ever]"
* @param string $value The value to place at the destination array key
*/
function parse_query_string_array(&$result, $k, $arrayKeys, $value)
{
if (!
preg_match_all('/\[([^\]]*)\]/', $arrayKeys, $matches))
return
$value;
if (!isset(
$result[$k])) {
$result[urldecode($k)] = array();
}
$temp =& $result[$k];
$last = urldecode(array_pop($matches[1]));
foreach (
$matches[1] as $k) {
$k = urldecode($k);
if (
$k === "") {
$temp[] = array();
$temp =& $temp[count($temp)-1];
} else if (!isset(
$temp[$k])) {
$temp[$k] = array();
$temp =& $temp[$k];
}
}
if (
$last === "") {
$temp[] = $value;
} else {
$temp[urldecode($last)] = $value;
}
}

/**
* Breaks a string into a pair for a common parsing function.
*
* The string passed in is truncated to the left half of the string pair, if any, and the right half, if anything, is returned.
*
* An example of using this would be:
* <code>
* $path = "Account.Balance";
* $field = string_pair($path);
*
* $path is "Account"
* $field is "Balance"
*
* $path = "Account";
* $field = string_pair($path);
*
* $path is "Account"
* $field is false
* </code>
*
* @return string The "right" portion of the string is returned if the delimiter is found.
* @param string $a A string to break into a pair. The "left" portion of the string is returned here if the delimiter is found.
* @param string $delim The characters used to delimit a string pair
* @param mixed $default The value to return if the delimiter is not found in the string
* @desc
*/
function string_pair(&$a, $delim='.', $default=false)
{
$n = strpos($a, $delim);
if (
$n === false)
return
$default;
$result = substr($a, $n+strlen($delim));
$a = substr($a, 0, $n);
return
$result;
}

?>
Rectangle 27 0

"INSERT INTO public.someTable (someField) VALUES (':someField');"

Once PDO was done with it it ended up looking like this:

"INSERT INTO public.someTable (someField) VALUES (''Y'');"

Correct use of the prepare() function is:

$stmt = $db->prepare("INSERT INTO public.someTable (someField) VALUES (:someField);");

string - Postgres/PHP PDO::PDOStatement->bindParam() to character(1) f...

php string postgresql pdo prepared-statement
Rectangle 27 0


@normadize - that is a nice start, but it fails on situations where a field is empty but quoted (returning a string with one double quote instead of an empty string) and cases like """""foo""""" that should result in ""foo"" but instead return "foo". I also get a row with 1 empty field at the end because of the final CRLF in the CSV. Plus, I don't really like the !!Q!! magic or urlencoding to get around things. Also, \R doesn't work in pcre on any of my php installations.

Here is my take on this, without anonymous functions (so it works on PHP < 5.3), and without your options (because I believe the only correct way to parse according to the RFC would be $skip_empty_lines = false and $trim_fields = false).

//parse a CSV file into a two-dimensional array
//this seems as simple as splitting a string by lines and commas, but this only works if tricks are performed
//to ensure that you do NOT split on lines and commas that are inside of double quotes.
function parse_csv($str)
{
//match all the non-quoted text and one series of quoted text (or the end of the string)
//each group of matches will be parsed with the callback, with $matches[1] containing all the non-quoted text,
//and $matches[3] containing everything inside the quotes
$str = preg_replace_callback('/([^"]*)("((""|[^"])*)"|$)/s', 'parse_csv_quotes', $str);

//remove the very last newline to prevent a 0-field array for the last line
$str = preg_replace('/\n$/', '', $str);

//split on LF and parse each line with a callback
return array_map('parse_csv_line', explode("\n", $str));
}

//replace all the csv-special characters inside double quotes with markers using an escape sequence
function parse_csv_quotes($matches)
{
//anything inside the quotes that might be used to split the string into lines and fields later,
//needs to be quoted. The only character we can guarantee as safe to use, because it will never appear in the unquoted text, is a CR
//So we're going to use CR as a marker to make escape sequences for CR, LF, Quotes, and Commas.
$str = str_replace("\r", "\rR", $matches[3]);
$str = str_replace("\n", "\rN", $str);
$str = str_replace('""', "\rQ", $str);
$str = str_replace(',', "\rC", $str);

//The unquoted text is where commas and newlines are allowed, and where the splits will happen
//We're going to remove all CRs from the unquoted text, by normalizing all line endings to just LF
//This ensures us that the only place CR is used, is as the escape sequences for quoted text
return preg_replace('/\r\n?/', "\n", $matches[1]) . $str;
}

//split on comma and parse each field with a callback
function parse_csv_line($line)
{
return array_map('parse_csv_field', explode(',', $line));
}

//restore any csv-special characters that are part of the data
function parse_csv_field($field) {
$field = str_replace("\rC", ',', $field);
$field = str_replace("\rQ", '"', $field);
$field = str_replace("\rN", "\n", $field);
$field = str_replace("\rR", "\r", $field);
return $field;
}
Rectangle 27 0

1. The enclosure parameter is the character the encapsulates the data for a specific field, or "index". By default, the parameter is a ", which means that you can "enclose" strings in " characters.

1,2,"this is three",4

2. Per a comment, you're calling fgetcsv($handle, 10000, ','). It's possible, maybe, that the line(s) you're reading are longer than 10000 characters. Try changing the length to 0, which will be "no limit" and see if that helps. Another solution would be to try wrapping the column's value in double-quotes.

while (($data = fgetcsv($handle, 10000, ',')) !== FALSE) {
$title = $data[0];

csv - PHP fgetcsv() delimiter - Stack Overflow

php csv fgetcsv readfile
Rectangle 27 0

The reason your shuffle call isn't working is your fields are delimited by | characters, but you're exploding on ". There's no " in those lines of text, so explode returns a single string inside a single element array - shuffling a 1 element array is rather pointless.

I need the shuffle lines, not first id. I'm add ; to end of the strings, and change explode to $masStr[$i]=explode(';',trim($qOpen[$i]));. But shuffle not work(

php - How to make shuffle of the string and include data from second f...

php shuffle