كتابة نص بلغة PHP يقبل قيمة نصية وله 3 وظائف

كتابة نص بلغة PHP يقبل قيمة نصية وله 3 وظائف : 

أ. الأحرف (): إرجاع عدد الأحرف.

ب. الكلمات (): إرجاع عدد الكلمات.

ج. الأسطر (): إرجاع عدد الأسطر.

Write a PHP class "Text" that accept a text value and have 3 methods:
a. characters(): Return the number of characters.
b. words(): Return the number of words.
c. lines(): Return the number of lines.
Usage Example: $text = new Text("This is a dummy text."); $words = $text->words();

الرجاء الإفادة فضلاً 


ziyad_kamal أضف ردا
  1. يمكنك إستخدام strlen لكي تعرف عدد الحروف في النص
  2. يمكنك إستخدام str_word_count لكي تعرف عدد الكلمات في النص
  3. يمكنك إستخدام count , explode لكي تعرف عدد الأسطر في النص و لاحظ أن explode تقوم بنقسيم النص عند كل سطر

كما في المثال الأتي

class Text 
{
  public string $text;

  public function __construct($text)
  {
    $this->text = $text;
  }

  public function characters_num()
  {
    return strlen($this->text);
  }

  public function words_num()
  {
    return str_word_count($this->text);
  }

  public function lines_num()
  {
    return count(explode("\n", $this->text));
  }
}

// يمكنك إستخدامها كالأتي

$text = new Text("your string");

$chars_num = $text->charachters_num();
$words_num = $text->words_num();
$lines_num = $text->lines_num();

بوركت 

وجزاك الله خيراً 

$text = new Text("your string");

$chars_num = $text->charachters_num();
$words_num = $text->words_num();
$lines_num = $text->lines_num();

لو أردنا أن نجعل هذا البرنامج يعمل مع

<!DOCTYPE HTML>  
<html>
	<head>
	</head>
	<body>  
		<h2>Text Analyzer</h2>
		<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>"> 
		  <textarea name="comment" rows="5" cols="40"></textarea>
		  <br><br>
		  <input type="submit" name="submit" value="Analyzer!">  
		</form>
	</body>
</html>

فورم ادخال نص وارساله ليرجع لنا قيم البرنامج

ماذا يمكن أن نضيف في الكود