PHP Strings

1 min read

A string is a sequence of characters, like “Hello world!”. –  STRING бол “Сайн уу ертөнц” Гэх мэт тэмдэгтүүдийн дараалал юм.

PHP String Functions – PHP үг өгүүлбэрийн функц #

In this chapter we will look at some commonly used functions to manipulate strings. – Энэ бүлэгт бид үг өгүүлбэрийдийг удирдах зарим түгээмэл функцуудыг авч үзэх болно.


strlen() – Return the Length of a String – Үг өгүүлбэрийн уртыг буцаана. #

The PHP strlen() function returns the length of a string. – PHP strlen() функц нь  үг өгүүлбэрийн уртыг буцаана.

Example #

Жишээ #

Return the length of the string “Hello world!”: – “Hello world!” гэсэн  өгүүлбэрийн уртыг буцаана:

<?php echo strlen("Hello world!"); // outputs 12 ?>

str_word_count() – Count Words in a String – Үгсийг өгүүлбэрт  тоолох #

The PHP str_word_count() function counts the number of words in a string. – PHP str_word_count() нь Өгүүлбэрийн үгсийн тоог тоолно.

Example #

Жишээ #

Count the number of word in the string “Hello world!”: – “Hello world!” гэсэн өгүүлбэрийг үгсийг тоолно уу:

<?php echo str_word_count("Hello world!"); // outputs 2 ?>

strrev() – Reverse a String – Өгүүлбэрийг тонгоргох #

The PHP strrev() function reverses a string. – PHP strrev() функц нь өгүүлбэрийг тонгорго.

Example #

Жишээ #

Reverse the string “Hello world!”: – “Hello world!” гэсэн өгүүлбэрийг тонгоргоно уу:

<?php echo strrev("Hello world!"); // outputs !dlrow olleH ?>

strpos() – Search For a Text Within a String – Өгүүлбэр доторх текстийг хайх #

The PHP strpos() function searches for a specific text within a string. If a match is found, the function returns the character position of the first match. If no match is found, it will return FALSE. – PHP strpos() функц нь өгүүлбэр доторх тодорхой текстийг хайж олдог. Хэрэв таарч байвал функц нь эхний тэмдэгтийн байрлалыг тонгоргоно. Хэрэв тохирох зүйл олдохгүй бол ХУДАЛ буцаах болно.

Example #

Жишээ #

Search for the text “world” in the string “Hello world!”: – “Hello world!” гэсэн өгүүлбэрээс “world” гэсэн текстийг хайх:

<?php echo strpos("Hello world!", "world"); // outputs 6 ?>

Tip: The first character position in a string is 0 (not 1).

Зөвлөгөө: Өгүүлбэр дэх тэмдэгийн эхний байрлал нь 0 (1 биш)


str_replace() – Replace Text Within a String – Текстийг өгүүлбэр дотор оруулах  #

The PHP str_replace() function replaces some characters with some other characters in a string. – PHP str_replace() функц нь зарим тэмдэгтүүдийг өгүүлбэрт байгаа бусад тэмдэгтүүдээр орлуулдаг.

Example #

Жишээ #

Replace the text “world” with “Dolly”: – “world” гэсэн үгийг “Dolly” -оор солино уу:

<?php echo str_replace("world", "Dolly", "Hello world!"); // outputs Hello Dolly! ?>

Exercise: #

Дасгал: #

Get the length of the string “Hello World!”. – “Hello World!” гэсэн өгүүлбэрийн уртыг авна уу 

echo ("Hello World!");

Powered by BetterDocs

Leave a Reply