Carlo Antonelli
Carlo Antonelli

Reputation: 3

How to read multiple values in a single POST variable

I have a POST variable 'IMG' that has multiple values like this example:

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

So the three URL of the images are separated by | but are in a single POST variable.

What is the best method to properly read and handle these values? I need to have these values as separate variables or as an array to properly handle them.

Upvotes: 0

Views: 282

Answers (3)

Phan Hero
Phan Hero

Reputation: 325

I think there is a way is to check if POST variable IMG exists first. Then explode its contents using PHP explode() function.

$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
}

Upvotes: 0

dmuensterer
dmuensterer

Reputation: 1885

You can simply explode() your string using | as the delimiter:

<?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

Upvotes: 2

lbrandao
lbrandao

Reputation: 4373

One option:

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

Upvotes: 0

Related Questions