- PHP String Functions - PHP үг өгүүлбэрийн функц
- strlen() - Return the Length of a String - Үг өгүүлбэрийн уртыг буцаана.
- str_word_count() - Count Words in a String - Үгсийг өгүүлбэрт тоолох
- strrev() - Reverse a String - Өгүүлбэрийг тонгоргох
- strpos() - Search For a Text Within a String - Өгүүлбэр доторх текстийг хайх
- str_replace() - Replace Text Within a String - Текстийг өгүүлбэр дотор оруулах
- Exercise:
- Дасгал:
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() функц нь  үг өгүүлбэрийн уртыг буцаана.
<?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() нь Өгүүлбэрийн үгсийн тоог тоолно.
<?php
echo str_word_count("Hello world!"); // outputs 2
?>				<?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() функц нь өгүүлбэр доторх тодорхой текстийг хайж олдог. Хэрэв таарч байвал функц нь эхний тэмдэгтийн байрлалыг тонгоргоно. Хэрэв тохирох зүйл олдохгүй бол ХУДАЛ буцаах болно.
<?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() функц нь зарим тэмдэгтүүдийг өгүүлбэрт байгаа бусад тэмдэгтүүдээр орлуулдаг.
<?php
echo str_replace("world", "Dolly", "Hello world!"); // outputs Hello Dolly!
?>
				