我正在使用wordpress中的一些代码和一个插件将php放在文本框中。
但出于某种原因使用此行
print_r($csv[0][1]);
它没有显示该值,只显示空白
更新:这是我正在使用的其余代码,我把它放在page.php文件中
<?PHP
function readCSV($csvFile){
$file_handle = fopen($csvFile, 'r');
while (!feof($file_handle) ) {
$line_of_text[] = fgetcsv($file_handle, 1024);
}
fclose($file_handle);
return $line_of_text;
}
// Set path to CSV file
$csvFile = 'https://docs.google.com/spreadsheets/d/1oC88LWXn4SgvVzK3wQAojXk7UM5tDjuWuMZDAjQTGjw/pub?gid=0&single=true&output=csv';
// $csv = readCSV($csvFile);
// echo '<pre>';
// print_r($csv);
// echo '</pre>';
?>
答案 0 :(得分:1)
我的工作正常
function _load_google_csv( $atts ) {
extract( shortcode_atts( array(
'position' => '',
), $atts ) );
ob_start();
// store spreadsheet content into variable
$data = file_get_contents('https://docs.google.com/spreadsheets/d/1oC88LWXn4SgvVzK3wQAojXk7UM5tDjuWuMZDAjQTGjw/pub?gid=0&single=true&output=csv');
// Convert Comma separted String to Array
$data = explode(',', $data );
// Remove Comment to know the array position of the value you want to pull
#echo '<pre>', print_r( $data, 1), '</pre>';
/* The first two array value are like this,
[0] => 5X5
[1] => Only 1 Left
5X10
You see the second array value is "Only 1 Left (Newline) 5X10" and since you don't
want to pull 5X10, this can be remove from the string using str_replace function,
refer to http://www.w3schools.com/php/func_string_str_replace.asp, you may also want
to improve your CSV format to avoid using of str_replace
Doing it step by step
#get the second array value
$array_1 = $data[1] // Value of $array_1 is "Only 1 Left (Newline) 5X10"
# Remove "5X10" from $array_1 value using str_replace();
$desired_value = str_replace('5X10', '', $array_1);
echo $desired_value; // $desired_value Will now output a value of "Only 1 Left"
*/
echo '<pre>', print_r( str_replace('5X10', '', $data[$position]), 1), '</pre>';
return ob_get_clean();
}
add_shortcode('print_csv','_load_google_csv');
短代码可以像这样使用,但是这种方法会出现问题,因为str_replace不能处理其他数组值,您可能需要重新格式化CSV以正确分离每个数据
[print_csv position="0"] // Return the first value of the array
[print_csv position="1"] // Return the second value of the array
答案 1 :(得分:0)
首先尝试print_r($csv);
以确保$ csv已设置且不为空。
然后弄清楚什么是错的!
您也可以使用isset进行检查 例如:
if(!isset($csv[0][1]))
echo 'I am NULL';
答案 2 :(得分:0)
$csv = array_filter($csv);
if (!empty($csv)) {
// Your code goes here
}
array_filter()函数的默认行为将从数组中删除所有等于null,0,&#39;&#39;&#39;或者是假的。
否则在您的特定情况下,如果至少有一个元素,则empty()构造将始终返回true,即使使用&#34; empty&#34;值。
答案 3 :(得分:0)
这段代码工作正常。这给了我一个数组。
<?PHP
function readCSV($csvFile){
$file_handle = fopen($csvFile, 'r');
while (!feof($file_handle) ) {
$line_of_text[] = fgetcsv($file_handle, 1024);
}
fclose($file_handle);
return $line_of_text;
}
// Set path to CSV file
$csvFile = 'https://docs.google.com/spreadsheets/d/1oC88LWXn4SgvVzK3wQAojXk7UM5tDjuWuMZDAjQTGjw/pub?gid=0&single=true&output=csv';
$csv = readCSV($csvFile);
echo '<pre>';
print_r($csv);
echo '</pre>';
?>
输出如下:
[0] => Array
(
[0] => 5X5
[1] => Only 1 Left
)
......所以
答案 4 :(得分:0)
您无法在WordPress后端编辑器中使用PHP。也许你可以使用插件,但不是开箱即用。
最简单的解决方案是创建一个短代码。然后,您可以在编辑器中使用[input type="text" name"from"]
之类的内容。
function readCSV($csvFile){
$file_handle = fopen($csvFile, 'r');
while (!feof($file_handle) ) {
$line_of_text[] = fgetcsv($file_handle, 1024);
}
fclose($file_handle);
return $line_of_text;
}
第二个函数调用第一个,
function input_func( $atts ) {
// Set path to CSV file
$csvFile = 'https://docs.google.com/spreadsheets/d/1oC88LWXn4SgvVzK3wQAojXk7UM5tDjuWuMZDAjQTGjw/pub?gid=0&single=true&output=csv';
$csv = readCSV($csvFile);
echo '<pre>';
print_r($csv);
echo '</pre>';
}
add_shortcode( 'input', 'input_func' );
答案 5 :(得分:0)
检查您的PHP安装是否有allow_url_fopen = on
,如果没有,请将其激活或下载该文件作为本地文件。
fopen 无法从网址读取更多信息:http://php.net/manual/es/function.fopen.php