Resolve PHP post string [closed] - php

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have the following post string:
Name=John&Age=33&Question1=What's+your+gender%3F&Answer1=Male&Question2=What's
+your+education%3F&Answer2=Graduate
I'm going to have randomly generated amount of questions each time so I won't be able to know number of passed variables. Thus, I am searching for a solution something like:
foreach($postItem as $varr) {
echo $varr["name"].": ".$varr["value"]."<br />";
}

Post them as answer[1], answer[2] etc instead of answer1, answer2, and PHP will turn them into an array for you.

//parse query string to an array ($output)
parse_str($postString, $output);
echo $output['Name'] . ': ' . $output['Age'] . '<br />';
//process questions
$i = 1;
while (array_key_exists('Question' . $i, $output)) {
echo $output['Question' . $i] . ': ' . $output['Answer' . $i] . '<br />';
$i++;
}
function used http://php.net/parse_str

Related

How to divide CSV to two different Strings in PHP [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I am trying to divide CSV into Two String LHS_String and RHS_String. For Example, I have the below CSV -
customer^id,record^id
customer^fname,record^first_name
cusomer^gender,record^gender
The Output should be like -
LHS_String : customer^id
RHS_String : record^id
Short answer, read over http://php.net/manual/en/function.explode.php
Example:
<?php
$data = "customer^id,record^id\r\n";
$data .= "customer^fname,record^first_name\r\n";
$data .= "cusomer^gender,record^gender\r\n";
$p1 = explode("\r\n", $data);
$p2 = array();
foreach($p1 as $key => $line){
$p2[$key] = explode(",", $line);
}
echo "LHS_String : " . $p2[0][0] . "\r\n";
echo "RHS_String : " . $p2[0][1] . "\r\n";
?>

String Length Issue [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 5 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
I have four string length statements I am trying to validate,but only two are working.
$name = "mike";
$age = "54";
$city = "Chicago";
$building = "Madadnock";
$lengthOfString1 = strlen ( $name );
echo "String length of 'name' " . $lengthOfString1 . "</br>";
$lengthOfString2 = strlen ( $age );
echo "String length of 'age' " . $lengthOfString2 . "</br>";
$lengthOfString3 = strlen ( $city );
echo "String length of 'city' " . $lengthOfstring3 . "</br>";
$lengthOfString4 = strlen ( $building );
echo "String length of 'building' " . $lengthOfstring4 . "</br>";
The top two are the only ones that work. Any help would be great!
variables in PHP are case sensitive
$lengthOfString4 vs $lengthOfstring4
^ ^
The problem lies in variable names. The variable names in PHP are case-sensitive.
$lengthOfString3 = strlen ( $city );
echo "String length of 'city' " . $lengthOfString3 . "</br>";
$lengthOfString4 = strlen ( $building );
echo "String length of 'building' " . $lengthOfString4 . "</br>";
Fixed it.

php include images randomly, echo and seperate by commas? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
i want to include files, and if exist echo the filename with extension like 1.jpg, but it need to be randomly and seperated by commas like these
('1.jpg','2.jpg','3.jpg','4.jpg')
, but if some of pictures dont exist in specified follder, need to echo manually, example if there are only three images on folder, need to be like this
( 1.jpg,'2.jpg','3.jpg','default.jpg')
, Hope someone understand what im asking for.
Try this:
<?php
$images = glob('/images/*.{jpeg,gif,png}', GLOB_BRACE);
shuffle($images);
$out = '(';
foreach($images as $img)
{
$out .= '"';
$out .= file_exists($img) ? $img : 'default.jpg';
$out .= '",';
}
$out .= ')';
echo $out;
?>
<?php
// set minimum number of images to show
$show_min=4;
// define manual images
$manual[]="1.jpg";
$manual[]="2.jpg";
$manual[]="3.jpg";
$manual[]="4.jpg";
// shuffle images
$images_found = glob("images/bg/*.{jpg,png,gif}", GLOB_BRACE)
shuffle($images_found);
$result = '(';
foreach($images_found as $image)
{
$result.='"' . $image . '",';
}
if (count(images_found) < $show_min)
{
for($i=0;$i<($show_min-count(images_found));$i++)
$result.='"' . $manual[$i] . '",';
}
$result .= ')';
//fix for last comma
$result=str_replace(",)", ")", $result);
echo $result;
?>
Hope it works.

Please specify how to implement this code between echo ' '; [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have using this code between echo "";
<?php
for ($i=0; $i<$tot; $i++)
{
$rt = date ( "d-m-Y", $y[$i] ) ;
}
?>
Please specify how to implement this code between echo "";
<?php
$output = 'for ($i=0; $i<$tot; $i++){ ';
$output .= '$rt = date ( \'d-m-Y\', $y[$i] );';
$output .= '}';
echo $output;
?>
For readability I separated it into 4 lines, but you can see how it could easily be refactored into one echo statement.
The key idea here is that by declaring strings with ' instead of ", php will not interpret the $ signs as variables. Additionally, in order to include single quotes within the string itself, a backslash escape character is required as in \'d-m-Y\'

Get part of string in PHP without explode [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have the following string
$s = "hellomyname";
$result = ...
How can I get the "my" out of this the fastest way possible?
You have to find the position of ((my)) in your string with strpos() function and then fetch the word with substr().
here is an example:
<?php
$s1 = 'laldkfjamydnadjacv zdvzkv';
$pos = strpos($s1, 'my');
echo 'First Position---> ' . $pos;
$my1 = substr($s1, $pos, 2);
echo '<br /> this is result---> ' . $my1;
//second test
$s2 = 'hellomyname';
$pos2 = strpos($s2, 'my');
echo '<br />Second position---> ' . $pos2;
$my2 = substr($s2, $pos2, 2);
echo '<br />this is second result---> ' . $my2;
?>
and this is result:
First Position---> 8
this is result---> my
Second position---> 5
this is second result---> my
I think you're looking for substr($s, 5, 2).
Explode (Delimiter, text)
In your case can use:
$result = explode('my', 'hellomyname'); // array([0] => 'hello', [1] => 'name');
If you need get the last value you can put end():
$result = end(explode('my', 'hellomyname')); //name
Reference
http://www.php.net/manual/pt_BR/function.explode.php
http://us1.php.net/end

Categories