---
title: PHP Strings
description: "> A string is a sequence of characters, like “Hello world!”. –  STRING бол “Сайн уу ертөнц” Гэх мэт тэмдэгтүүдийн дараалал юм. > ## PHP String Functions – PHP үг өгүүлбэрийн функц > > > > In this chapter we…"
url: "https://apprentice.mn/docs/php-strings/"
updated: "2021-07-07"
category: PHP
---

# PHP Strings

> 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!");
```

         [https://www.addtoany.com/add_to/facebook?linkurl=https%3A%2F%2Fapprentice.mn%2Fdocs%2Fphp-strings%2F&linkname=PHP%20Strings](https://www.addtoany.com/add_to/facebook?linkurl=https%3A%2F%2Fapprentice.mn%2Fdocs%2Fphp-strings%2F&linkname=PHP%20Strings)[https://www.addtoany.com/add_to/mastodon?linkurl=https%3A%2F%2Fapprentice.mn%2Fdocs%2Fphp-strings%2F&linkname=PHP%20Strings](https://www.addtoany.com/add_to/mastodon?linkurl=https%3A%2F%2Fapprentice.mn%2Fdocs%2Fphp-strings%2F&linkname=PHP%20Strings)[https://www.addtoany.com/add_to/email?linkurl=https%3A%2F%2Fapprentice.mn%2Fdocs%2Fphp-strings%2F&linkname=PHP%20Strings](https://www.addtoany.com/add_to/email?linkurl=https%3A%2F%2Fapprentice.mn%2Fdocs%2Fphp-strings%2F&linkname=PHP%20Strings)[https://www.addtoany.com/share](https://www.addtoany.com/share)
