Example #1
0
<?php

/*
	Using PHP, have the function WordCount(str) take the str string parameter
	being passed and return the number of words the string contains (ie.
	"Never eat shredded wheat" would return 4). Words will be separated by single spaces. 
*/
function WordCount($str)
{
    $countWords = 0;
    $massWords = explode(" ", $str);
    for ($i = 0; $i < count($massWords); $i++) {
        if ($massWords[$i] != "") {
            $countWords++;
        }
    }
    return $countWords;
}
//Test
echo WordCount('Hello World');
$result_recover = mysql_query($sql_recover, $conn) or die(mysql_error());
$array_recover = mysql_fetch_array($result_recover);
$answer_id_recover = $array_recover['answer_id'];
$answer_response_recover = $array_recover['answer_response'];
$answer_id_edited_recover = $array_recover['answer_id_edited'];
$answer_time_edited_recover = $array_recover['answer_time_edited'];
if ($answer_id > 0 and $answer_response == NULL) {
    $sql_archive = "INSERT INTO intranet_tender_answers_archive (\n\t\t\t\t\t\tarchive_id,\n\t\t\t\t\t\tarchive_text,\n\t\t\t\t\t\tarchive_user,\n\t\t\t\t\t\tarchive_time,\n\t\t\t\t\t\tarchive_response\n\t\t\t\t\t\t) values (\n\t\t\t\t\t\t'NULL',\n\t\t\t\t\t\t'{$answer_response_recover}',\n\t\t\t\t\t\t'{$answer_id_edited_recover}',\n\t\t\t\t\t\t'{$answer_time_edited_recover}',\n\t\t\t\t\t\t'{$answer_id_recover}'\n\t\t\t\t\t\t)";
    $result_archive = mysql_query($sql_archive, $conn) or die(mysql_error());
}
unset($alertmessage);
// Begin to clean up the $_POST submissions
$answer_id = CleanNumber($_POST[answer_id]);
$answer_response = strip_tags($_POST[answer_response], "<p><ul><li><ol><strong><b><i><italic><u>");
$answer_feedback = strip_tags($_POST[answer_feedback], "<p><ul><li><ol><strong><b><i><italic><u>");
$word_count = WordCount(strip_tags($answer_response));
$answer_complete = $_POST[answer_complete];
$answer_response = str_replace("<p></p>", "", $answer_response);
// $answer_response = preg_replace('/\s\s+/', ' ',$answer_response);
$answer_response = addslashes(preg_replace('/(<p.+?)+(<\\/>)/i', '<p>', $answer_response));
$answer_feedback = str_replace("<p></p>", "", $answer_feedback);
if ($_POST[clear_format] == "yes") {
    $answer_response = strip_tags($answer_response, "<p>");
}
if ($word_count == 0 or $word_count == NULL) {
    unset($answer_time_edited);
    unset($answer_id_edited);
} else {
    $answer_time_edited = time();
    $answer_id_edited = $_COOKIE[user];
}
 $tender_client = $array['tender_client'];
 $tender_name = $array['tender_name'];
 $tender_date = $array['tender_date'];
 $tender_type = $array['tender_type'];
 $tender_source = $array['tender_source'];
 $tender_instructions = $array['tender_instructions'];
 $user_name_first = $array['user_name_first'];
 $user_name_second = $array['user_name_second'];
 $word_count_total = $word_count_total + $answer_wordcount;
 if ($tender_date > time() or $user_usertype_current > 3) {
     $tender_editable = "yes";
 } else {
     $tender_editable = "no";
 }
 if ($answer_words != NULL) {
     $word_count = WordCount($answer_words);
 } else {
     $word_count = "0";
 }
 if ($tender_editable == "yes" and $answer_id != $_GET[edit_question] and $_GET[edit_answer] == NULL and $_GET[edit_question] == NULL) {
     $answer_ref = $answer_ref . "&nbsp;<a href=\"index2.php?page=tender_view&amp;tender_id={$_GET['tender_id']}&amp;edit_question={$answer_id}#{$answer_id}\"><img src=\"images/button_edit.png\" alt=\"Edit\" /></a>";
 }
 if ($counter == 0) {
     echo "<h1>{$tender_name} ({$tender_type})</h1>";
     echo "<p class=\"submenu_bar\">";
     if ($user_usertype_current > 2) {
         echo "<a href=\"index2.php?page=tender_view&amp;question=add&amp;tender_id={$tender_id}\" class=\"submenu_bar\">Add Question</a>";
     }
     if ($_GET[edit_question] == NULL) {
         echo "<a href=\"popup_tender.php?tender_id={$tender_id}\" class=\"submenu_bar\">Printable View</a>";
     }
Example #4
0
<?php

//Challenge 13
//Have the function WordCount(str) take the str string parameter being passed and return the number of words the string contains (ie. "Never eat shredded wheat" would return 4).
//Words will be separated by single spaces.
function WordCount($str)
{
    $word_count = 0;
    // code goes here
    for ($count = 0; $count <= strlen($str) - 1; $count++) {
        $character = substr($str, $count, 1);
        if ($character == " " || $count == strlen($str) - 1) {
            $word_count++;
        }
    }
    return $word_count;
}
// keep this function call here
// to see how to enter arguments in PHP scroll down
echo WordCount(fgets(fopen('php://stdin', 'r')));
?>