Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making sure PHP substr finishes on a word not a character

Tags:

substring

php

I know how to use the substr function but this will happy end a string halfway through a word. I want the string to end at the end of a word how would I go about doing this? Would it involve regular expression? Any help very much appreciated.

This is what I have so far. Just the SubStr...

echo substr("$body",0,260); 

Cheers

like image 634
Cool Hand Luke Avatar asked Aug 05 '09 13:08

Cool Hand Luke


People also ask

How can I get the last letter of a word in PHP?

The strlen() is a built-in function in PHP which returns the length of a given string. It takes a string as a parameter and returns its length. It calculates the length of the string including all the whitespaces and special characters.

What is substr () in PHP and how it is used?

The substr() is a built-in function of PHP, which is used to extract a part of a string. The substr() function returns a part of a string specified by the start and length parameter. PHP 4 and above versions support this function.

How can I change the last character of a string in PHP?

You can use the PHP substr_replace() function to remove the last character from a string in PHP. $string = "Hello TecAdmin!"; echo "Original string: " . $string .

Which Substr function is used to extract characters?

MySQL SUBSTR() Function The SUBSTR() function extracts a substring from a string (starting at any position).


1 Answers

substr($body, 0, strpos($body, ' ', 260)) 
like image 178
Achshar Avatar answered Oct 06 '22 11:10

Achshar