PHP - Validation & Thread Add validation that checks: $title and $message both h
ID: 3800952 • Letter: P
Question
PHP - Validation & Thread
Add validation that checks:
$title and $message both have a minimum length of 3.
title has a maximum length of 32 characters.
$message has a maximum length of 52 characters.
If any of the three validation rules fail, print a status of 432 and an error message of what is wrong with the user input. The JSON response should look like:
If the user submitted a successful post, save the record using the provided Thread class. Set the timeposted set to when the item was submitted. Make the thread visible as soon as its submitted. Print a status code of 200. The JSON response should look like:
Add your code to newthread.php. Test your script by running:
ALSO convert all urls in the $message to HTML links and store the new result as message_html using the provided Thread class.
HINT: You'll need to modify the provided Thread class.
-------------------------------
newthread.php
------------------------
Thread.php
-------------------------------
Explanation / Answer
to validate the lengths of $title and $message variable the code is as follows:
$suc="status”: 432, “error”: “title field is too short
$unsuc="“status”: 200"
if (32>strlen($title)>3 && 52>strlen($message)>3)
{
echo json_encode($suc)
}
else
{
echo json_encode($unsuc)
}
to modify the urls in $message we need to use regex
$message_html = preg_replace($message, '<a href="$0" target="_blank" title="$0">$0</a>', $message_html);
echo $message_html;
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.