How to Count the Number of Words in String in Laravel?
Hi Friends,
This article goes in detailed on laravel count words in string. We will look at an example of how to count number of words in a string in laravel. This example will help you how to count words in a string in laravel blade. you'll learn laravel Str::wordCount. Let's see below example laravel str wordcount() example.
Str::wordCount is not a built-in method in Laravel. It seems that you're trying to find the word count of a string using Laravel's Str class, but there is no such method available.
To count the number of words in a string, you can use plain PHP functions like str_word_count. Here's an example:
let's see the simple example:
Laravel String Count Number of Words in Controller
you can use it with controller like this way:
Controller Code:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Str;
class UserController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index(Request $request)
{
$string = "Hello world! This is a string.";
$words = Str::wordCount($string);
dd($words);
}
}
Output:
6
Laravel String Count Number of Words in Blade File
you can use it with blade file like this way:
Blade File Code:
<p>{{ Str::wordCount("Hello world! This is a string.") }}</p>
Output:
6
I hope it can help you...
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- Laravel Check If String Starts with Specific Value Example
- How to Convert String to CamelCase in Laravel?
- How to Get Length of String in Laravel?
- Laravel Convert String to Uppercase Example
- Laravel Convert String to Lowercase Example
- Laravel Import Large CSV File Using Queue Example
- How to Replace String in Laravel?
- How to Install and Setup Supervisor in Ubuntu for Laravel?
- Laravel String Contains Case Insensitive Example
- Laravel Stripe Checkout Payment Integration Example
- How to Convert String to Array Conversion in Laravel?
- Laravel JQuery Ajax Loading Spinner Example
- Laravel Eloquent withSum() and withCount() Example