Lấy ký tự đầu tiên của mỗi từ trong chuỗi php

❮ Tham chiếu chuỗi PHP

Thí dụ

Chuyển đổi ký tự đầu tiên của mỗi từ thành chữ hoa

echo ucwords("Xin chào thế giới");
?>

Tự mình thử »


Định nghĩa và cách sử dụng

Hàm ucwords() chuyển ký tự đầu tiên của mỗi từ trong chuỗi thành chữ hoa

Ghi chú. Hàm này an toàn nhị phân

chức năng liên quan

  • ucfirst() - chuyển ký tự đầu tiên của chuỗi thành chữ hoa
  • lcfirst() - chuyển ký tự đầu tiên của chuỗi thành chữ thường
  • strtoupper() - chuyển đổi một chuỗi thành chữ hoa
  • strtolower() - chuyển đổi một chuỗi thành chữ thường

cú pháp

ucwords(chuỗi, dấu phân cách)

Giá trị tham số

Tham sốChuỗi mô tảBắt buộc. Chỉ định chuỗi để convertdelimitersOptional. Chỉ định ký tự tách từ

chi tiết kỹ thuật

Giá trị trả về. Trả về phiên bản stringPHP đã chuyển đổi. 4+Nhật ký thay đổi. PHP5. 4 - Đã thêm tham số dấu phân cách

Thêm ví dụ

Thí dụ

Chuyển đổi ký tự đầu tiên của mỗi từ thành chữ hoa, có thêm dấu tách từ tùy chỉnh

echo ucwords("Xin chào. thế giới", ". ");
?>

Tự mình thử »

❮ Tham chiếu chuỗi PHP

Nếu bạn cần viết hoa ký tự đầu tiên của mỗi từ trong một chuỗi trong PHP, bạn có thể sử dụng hàm ucwords(). Hàm này lấy một chuỗi làm đối số và trả về một chuỗi mới với ký tự đầu tiên của mỗi từ trong chuỗi ban đầu được viết hoa

Sao chép mã

đầu ra

Hello World

Để chuyển đổi chữ cái đầu tiên của mỗi từ trong Chuỗi thành chữ hoa

  1. Xác định một chuỗi có chứa một số từ
  2. Truyền chuỗi cho hàm ucwords() sẽ trả về chuỗi với mỗi từ có ký tự đầu tiên là chữ hoa
  3. in chuỗi

cú pháp

ucwords($str)

PHP >> Chuỗi >> Chữ hoa-ký tự đầu tiên của mỗi từ

Câu hỏi

Làm cách nào để bạn viết hoa ký tự đầu tiên của mỗi từ trong một chuỗi bằng PHP?

Câu trả lời

Sử dụng hàm PHP ucwords()

Đây là một ví dụ PHP cho thấy cách viết hoa ký tự đầu tiên của mỗi từ trong một chuỗi

Nguồn. (thí dụ. php)

 

đầu ra

$ php example.php
The Cat In The Hat

Người giới thiệu

// C++ program to remove the first5_______5_______69// C++ program to remove the first83// C++ program to remove the first64 // C++ program to remove the first85// C++ program to remove the first22// C++ program to remove the first87// C++ program to remove the first88// C++ program to remove the first89// C++ program to remove the first90 // C++ program to remove the first22// C++ program to remove the first92// C++ program to remove the first83 // C++ program to remove the first3// C++ program to remove the first4

    

    std;9 string FirstAndLast(string str)0

    

// and last character of each word in a string.01

#include3 // and last character of each word in a string.03// C++ program to remove the first64_______5_______64 // and last character of each word in a string.06 // C++ program to remove the first87

    

    // and last character of each word in a string.10_______5_______64 string FirstAndLast(string str)8// C++ program to remove the first4

    

    // and last character of each word in a string.16_______25_______17

    // and last character of each word in a string.16_______25_______20

 

// and last character of each word in a string.21

C#




// and last character of each word in a string.22

// and last character of each word in a string.

using // and last character of each word in a string.25

 

    4 // and last character of each word in a string.27

{

        7     8

    {

 

// C++ program to remove the first5_______5_______02

// C++ program to remove the first5_______25_______37#include5// C++ program to remove the first06

 

// C++ program to remove the first5_______5_______08

// C++ program to remove the first5_______5_______10_______5_______8// C++ program to remove the first4

 

// C++ program to remove the first5_______5_______14

// C++ program to remove the first5_______5_______16

// C++ program to remove the first5_______25_______51 // and last character of each word in a string.52// C++ program to remove the first79 // and last character of each word in a string.54

// C++ program to remove the first5_______52_______

#include9// and last character of each word in a string.58// C++ program to remove the first3_______5_______4

// C++ program to remove the first5_______86_______5

 

// C++ program to remove the first5_______22_______9 string FirstAndLast(string str)0

    namespace5

 

    string FirstAndLast(string str)2

    // C++ program to remove the first38     7 // C++ program to remove the first40 // and last character of each word in a string.74

    {

// C++ program to remove the first5_______5_______45string FirstAndLast(string str)8// C++ program to remove the first4

// C++ program to remove the first5_______25_______82

// C++ program to remove the first5_______25_______84

    namespace5

namespace5

 

// and last character of each word in a string.88

PHP




// and last character of each word in a string.89

// and last character of each word in a string.90

// and last character of each word in a string.

 

// and last character of each word in a string.92 // and last character of each word in a string.93// and last character of each word in a string.94#include6

{

    // C++ program to remove the first0

    // and last character of each word in a string.94 #include01_______5_______3// C++ program to remove the first4

    

    #include06 #include07

    #include09 #include07

    

    // and last character of each word in a string.4

    // and last character of each word in a string.6_______25_______7#include17_______26_______18#include17#include20#include21// and last character of each word in a string.7// and last character of each word in a string.94// C++ program to remove the first06#include17#include26

    {

// C++ program to remove the first5_______26_______3// and last character of each word in a string.7// and last character of each word in a string.94#include33#include17#include35#include5#include6

// C++ program to remove the first5_______52_______

#include9using0

#include9using2

#include9#include06 #include01#include47// and last character of each word in a string.7#include09#include50#include21// and last character of each word in a string.7#include09#include54

Làm cách nào để lấy chữ cái đầu tiên của mỗi từ trong chuỗi trong PHP?

Vì vậy, bạn có thể dễ dàng sử dụng $i[0] để tìm nạp chữ cái đầu tiên.

Làm cách nào để lấy ký tự đầu tiên của từ trong PHP?

mb_substr. Nó đưa vào xem xét mã hóa văn bản. Thí dụ. $first_character = mb_substr($str, 0, 1)
chất nền. Thí dụ. $first_character = chất nền($str, 0, 1);
Sử dụng dấu ngoặc ( [] ) Tránh vì nó đưa ra thông báo trong PHP 7. x và một cảnh báo trong PHP 8. x nếu chuỗi rỗng. Thí dụ. $first_character = $str[0];

Làm cách nào để lấy ký tự đầu tiên của chuỗi PHP?

Tùy chọn 1. chất nền . Vì vậy, trong hàm bên dưới, 0 là vị trí bạn muốn bắt đầu và 30 là số ký tự bạn muốn hiển thị từ đầu chuỗi.

Làm cách nào để lấy 3 ký tự đầu tiên của chuỗi trong PHP?

Bạn có thể sử dụng hàm substr như thế này. echo substr($myStr, 0, 5); Đối số thứ hai của substr là bạn muốn bắt đầu từ vị trí nào và đối số thứ ba là bao nhiêu . Lưu câu trả lời này.