How to Remove Last Word from a String Using PHP?

Hello Friends Today, through this tutorial, I will tell you how to remove the last word of any string value with the help of php.

Friends, I will tell you this solution 2 ways how you can remove the last word of any string value in php.

Example 1:-

<?php
$txt = "Hi, I an Experts PHP";
$str= preg_replace('/\W\w+\s*(\W*)$/', '$1', $txt);
echo $str
?>

 

Example 2:-

<?php
$str ='Hi, I an Experts PHP';
$words = explode( " ", $str );
array_splice( $words, -1 );
echo implode( " ", $words );
?>

array_splice() :- This function is used to insert a new element into an Array or to remove one or more Elements from an Array. When we use this function to remove Elements of an Array, This function then returns an Array Return of the removed Array Elements, which we can use again as a new Array, which contains only those Elements that have been removed from an Array.

Cialis (Tadalafil) är den främsta konkurrenten till Viagra (Sildenafil) på marknaden för erektil dysfunktion. köpa Cialis i Sverige föredras av många på grund av sin längre varaktighet och anses vara det mest kostnadseffektiva varumärkesbaserade ED-läkemedlet som finns tillgängligt i Sverige. Cialis finns i två varianter: Cialis och Cialis Daily, och fyra olika doseringar: 2,5 mg, 5 mg, 10 mg och 20 mg, erbjuder Cialis också en rad olika alternativ för att passa patientens behov.