Asked  7 Months ago    Answers:  5   Viewed   19 times

Can anyone help me with a solution that pulls the position and value of a random character from a given string using PHP. For example I have a a string variable $string = 'helloworld'; and would like to randomly select a character from $string and echo the character and its position.

 Answers

10
$str = 'helloworld';

$randomChar = $str[rand(0, strlen($str)-1)];

CodePad.

Wednesday, March 31, 2021
 
ManojGeek
answered 7 Months ago
86

You should point to your vendor/autoload.php at Settings | PHP | PHPUnit when using PHPUnit via Composer.

This blog post has all the details (with pictures) to successfully configure IDE for such scenario: http://confluence.jetbrains.com/display/PhpStorm/PHPUnit+Installation+via+Composer+in+PhpStorm

Related usability ticket: http://youtrack.jetbrains.com/issue/WI-18388

P.S. The WI-18388 ticket is already fixed in v8.0

Wednesday, March 31, 2021
 
ojrac
answered 7 Months ago
15

When you ask your question to the user, a question is randomly selected from the database.

Then, the user submits your form, another question is randomly selected, and that's the question you are using to check the answer, instead of the question you asked to the user.

You need to add an hidden input in your form, that contains the question id

<input type="hidden" name="question_id" value="<?php echo $question_id ?>" />

And then when you check the answer, be sure to fetch the right question from the database

The code would look like this

<?php

// Check user answer for previous question
if (isset($_POST['submit'])) {   
    // Retrieve the id of the question you asked
    $previous_question_id = (int) $_POST['question_id']; // cast to integer to prevent sql injection.

    // Query database
    $get_question = "SELECT * from questions_table where id = $previous_question_id";
    $result_get_question = mysqli_query($conn, $get_question);
    $row_get_question = mysqli_fetch_array($result_get_question);

    // Assign database response to variables
    $correct = $row_get_question['correct'];
    $selected_radio = $_POST['response'];

    if ($selected_radio == $correct)
        echo "THAT ANSWER IS CORRECT";
    else
        echo "THAT ANSWER IS WRONG!";
}


// Load new question to ask to the user
$get_question = "SELECT * from questions_table order by rand() limit 1";
$result_get_question = mysqli_query($conn,$get_question);
$row_get_question = mysqli_fetch_array($result_get_question);  

// assign thing we want to print in the template to variable
$question_id = $row_get_question['question_id'];
$question = $row_get_question['question'];
$a1 = $row_get_question['a1'];
$a2 = $row_get_question['a2'];
$a3 = $row_get_question['a3'];
$a4 = $row_get_question['a4'];

?>

<PASTE YOUR TEMPLATE HERE>
Saturday, May 29, 2021
 
tedders
answered 5 Months ago
79

On Mac OS X environment variables available in Terminal and for the normal applications can be different, check the related question for the solution how to make them similar.

Note that this solution will not work on Mountain Lion (10.8).

Saturday, May 29, 2021
 
Nate
answered 5 Months ago
88

There are several methods available, and they can sometimes be made to perform exactly the same task, like preg_replace/str_replace. But, perhaps you want to remove brackets only from the beginning or end of the string; in which case preg_replace works. But, if there could be several brackets, preg_replace can do the job too. But trim is easier and makes more sense.

preg_replace() - removes beginning and trailing brackets

$widget_id = preg_replace(array('/^[/','/]$/'), '',$widget_text);      

str_replace() - this removes brackets anywhere in the text

$widget_id = str_replace(array('[',']'), '',$widget_text);

trim() - trims brackets from beginning and end

$widget_id = trim($widget_text,'[]')

substr() - does the same as trim() (assuming the widget text does not include any closing brackets within the text)

$widget_id = substr($widget_text,
                    $start = strspn($widget_text, '['),
                    strcspn($widget_text, ']') - $start
             );
Monday, June 14, 2021
 
Troncoso
answered 5 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :