php get first letter of string

How to get the first word of a sentence in PHP?

I want to extract the first word of a variable from a string. For example, take this input:

php get first letter of string. Смотреть фото php get first letter of string. Смотреть картинку php get first letter of string. Картинка про php get first letter of string. Фото php get first letter of string

16 Answers 16

There is a string function (strtok) which can be used to split a string into smaller strings (tokens) based on some separator(s). For the purposes of this thread, the first word (defined as anything before the first space character) of Test me more can be obtained by tokenizing the string on the space character.

For more details and examples, see the strtok PHP manual page.

You can use the explode function as follows:

If you have PHP 5.3

That will always return the first word of the string, even if the string has just one word in it

The alternative is something like:

Or using explode, which has so many answers using it I won’t bother pointing out how to do it.

Even though it is little late, but PHP has one better solution for this:

php get first letter of string. Смотреть фото php get first letter of string. Смотреть картинку php get first letter of string. Картинка про php get first letter of string. Фото php get first letter of string

Similar to accepted answer with one less step:

php get first letter of string. Смотреть фото php get first letter of string. Смотреть картинку php get first letter of string. Картинка про php get first letter of string. Фото php get first letter of string

Just in case you are not sure the string starts with a word.

Using split function also you can get the first word from string.

Just use explode to get every word of the input and output the first element of the resulting array.

strtok is quicker than extract or preg_* functions.

personally strsplit / explode / strtok does not support word boundaries, so to get a more accute split use regular expression with the \w

This would split words with boundaries to a limit of 1.

Here are the varying results from 2 consecutive runs:

And the results after inverting the order of the functions:

Conclusion It turns out that the speed between these functions varies widely and is not as consistent between test runs as you might expect. According to these quick and dirty tests, any of the six chosen functions will get the job done in a reasonable amount of time. There are perturbations including other processes running that are interfering with the execution times. So just use whatever function makes the most practical and readable sense to you as a programmer. For the bigger programming picture, see Donald Knuth’s Literate Programming.

Источник

ucfirst

(PHP 4, PHP 5, PHP 7, PHP 8)

ucfirst — Make a string’s first character uppercase

Description

Returns a string with the first character of string capitalized, if that character is alphabetic.

Note that ‘alphabetic’ is determined by the current locale. For instance, in the default «C» locale characters such as umlaut-a (ä) will not be converted.

Parameters

Return Values

Returns the resulting string.

Examples

Example #1 ucfirst() example

See Also

User Contributed Notes 35 notes

Simple multi-bytes ucfirst():

A proper Turkish solution;

?>

it also check is mb support enabled or not

This is what I use for converting strings to sentence case:

print sentence_case ( ‘HMM. WOW! WHAT?’ );

// Outputs: «Hmm. Wow! What?»
?>

Here’s a function to capitalize segments of a name, and put the rest into lower case. You can pass the characters you want to use as delimiters.

Implementation of multi-bytes ucfirst for «multiword»-strings (module mbstring is required):

Improved method of capitalizing first characters of sentences.
The first two manipulations (double spaces & all caps) are optional so can be removed without harm.

plemieux’ function did not work for me without passing the encoding to every single mb function (despite ini_set(‘default_charset’, ‘utf-8’) at the top of the script). This is the example that works in my application (PHP 4.3):

For some reason this worked for me.

Mac OS 10.5.1
PHP 5.2.6

Here is the fixed function for Turkish alphabet..

for anyone wanting to ucfirst each word in a sentence this works for me:

For lithuanian text with utf-8 encoding I use two functions (thanks [mattalexxpub at gmail dot com] and Svetoslav Marinov)

My version, converst first letter of the first word in the string to uppercase

public function mb_ucfirst($str) <
$aParts = explode(» «,$str);
$firstWord = mb_convert_case($aParts[0],MB_CASE_TITLE,»UTF-8″);
unset($aParts[0]);

I made a small change. Now it takes care of points in numbers

if you want to ucfirst for utf8 try this one:

( «UTF-8» );
mb_regex_encoding ( «UTF-8» );

function RemoveShouting($string)
<
$lower_exceptions = array(
«to» => «1», «a» => «1», «the» => «1», «of» => «1»
);

$higher_exceptions = array(
«I» => «1», «II» => «1», «III» => «1», «IV» => «1»,
«V» => «1», «VI» => «1», «VII» => «1», «VIII» => «1»,
«XI» => «1», «X» => «1»
);

Using this function for Turkish language is won’t work because of multi-byte characters. But you can use some tricks:

here is how mb_ucfirst should be implemented in userland

@adefoor, Ken and Zee

This is a simple code to get all the ‘bad words’, stored in a database, out of the text. You could use str_ireplace but since that’s installed on PHP5 only, this works as well. It strtolowers the text first then places capitals with ucfirst() where it thinks a capital should be placed, at a new sentence. The previous sentence is ended by ‘. ‘ then.

Ah, the last code were spoiled, here is the fixed one:

?>

So, this function changes also other letters into uppercase, ucfirst() does only change: a-z to: A-Z.

Note: the return for this function changed in versions 4.3 when a string is passed of length 0. In 4.3 a string of length 0 is returned.

Results for 4.3:
string(0) «» string(4) «Owen»

In the event you sort of need multiple delimiters to apply the same action to, you can preg_replace this «second delimiter» enveloping it with your actual delimiter.

A for instance, would be if you wanted to use something like Lee’s FormatName function in an input box designed for their full name as this script was only designed to check the last name as if it were the entire string. The problem is that you still want support for double-barreled names and you still want to be able to support the possibility that if the second part of the double-barreled name starts with «mc», that it will still be formatted correctly.

This example does a preg_replace that surrounds the separator with your actual delimiter. This is just a really quick alternative to writing some bigger fancier blah-blah function. If there’s a shorter, simpler way to do it, feel free to inform me. (Emphasis on shorter and simpler because that was the whole point of this.) 😀

Here’s the example. I’ve removed Lee’s comments as not to confuse them with my own.

Источник

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *