如何读取单个 POST 变量中的多个值

How to read multiple values in a single POST variable

我有一个 POST 变量 'IMG',它有多个值,如下例所示:

IMG=url.com/img1.png|url.com/img2.png|url.com/img3.png&...

因此三个 URL 图像被 | 分隔,但在单个 POST 变量中。

正确读取和处理这些值的最佳方法是什么? 我需要将这些值作为单独的变量或数组来正确处理它们。

您可以使用 | 作为分隔符简单地 explode() 您的字符串:

<?php
$urls = explode("|", $_POST['IMG']);

echo $urls[0]; // url.com/img1.png
echo $url[1]; // url.com/img2.png
echo $url[2]; // url.com/img3.png

一个选项:

$images = explode('|', $_POST['IMG']);

我认为有一种方法是先检查 POST 变量 IMG 是否存在。然后使用 PHP explode() 函数分解其内容。

$image_urls = array(); //blank array assuming there are no image URLs
if(isset($_POST['IMG']))
{
  $image_urls = explode('|', $_POST['IMG']); 
}


//Below code will check if the array actually has image URL parts from 
//POST variable IMG
if(count($image_urls) > 0)
{
  //your code to process the images
}