Effect of water st
This disclosure re
Nike Air Jordan 6
NO. 07-05-0441-CR
The use of a "Birm
Q:
What's the qui
The present invent
Drug-delivery syst
Q:
How to pass a
In its latest cracQ:
How to read a string variable as a string and not as an array in php
I am parsing string variables using the $_GET function to obtain the variable. But every string value is an array which I want to remove. I cannot parse it manually as it has different length.
E.g.
$title="DSC_0001";
$image="DSC_0001_01.jpg";
$description="this is description";
$type="image";
The result of $title contains DSC_0001 instead of "DSC_0001". The result of $image contains DSC_0001_01.jpg instead of DSC_0001_01.jpg. The result of $type contains string instead of "image".
This is how I am calling this functions:
if($_SERVER['REQUEST_METHOD'] == 'POST'){
$type = $_POST['type'];
$title = $_POST['title'];
$image = $_POST['image'];
$description = $_POST['description'];
}
This is how I parse the values of the post function
How can I parse the values and echo them?
Please note that:
echo $_POST['title'][0]
echo $_POST['title'][1]
echo $_POST['title'][2]
echo $_POST['title'][3]
and so on.....
How can I extract "DSC_0001", "DSC_0001_01.jpg" and "this is description" from these values?
It must also work for "this is string", "this is test" and "this is test.jpg"
A:
As you want to parse the whole $_POST it may be helpful to read its documentation. The result you are getting are the keys of $_POST which is array as default. You can check the keys of $_POST using print_r($_POST);.
Now if you want to retrieve values for each key, then use the $_POST[$key] array. Example:
$_POST['title']; // gives you the array/arraystring or boolean as per the key
So to retrieve value use:
echo $_POST['title'][0]; // returns DSC_0001
If you want all elements use foreach:
foreach ($_POST as $key=>$value) {
echo $key; // print each element of your POST array.
}
The code to retrieve your post variables:
$value) {
echo $key; // print your post variables
}
?>
Hope this helps you. Thanks.
A:
PHP is an interpreted language that parses its statements one by one.
For example:
if ($_SERVER['REQUEST_METHOD'] == 'POST'){
$type = $_POST['type'];
$title = $_POST['title'];
$image = $_POST['image'];
$description = $_POST['description'];
}
In above code you have two ways to access these variables.
1. One way is to access these variables using index. For example:
echo $_POST['title'][0]; // will give you "DSC_0001"
echo $_POST['image'][0]; // will give you "DSC_0001_01.jpg"
echo $_POST['type'][0]; // will give you "image"
echo $_POST['description'][0]; // will give you "this is description"
2. Second way is using keys.
echo $_POST['title']->[0]; // will give you "DSC_0001"
echo $_POST['image']->[0]; // will give you "DSC_0001_01.jpg"
echo $_POST['type']->[0]; // will give you "image"
echo $_POST['description']->[0]; // will give you "this is description"
In the last two lines of code -> means using keys.
If you access like this:
$post = array('title'=>'title', 'image'=>'image', 'type'=>'type', 'description'=>'description');
And echo $post[0]; will give you only title key, echo $post['title']; will give you title value.
Hope this will help you. Happy coding.
A:
I guess you can do it in this way: