A detailed introduction to PHP's commonly used string intrinsic functions

Source: Internet
Author: User
Tags rtrim
This chapter describes several commonly used PHP string intrinsics. The following PHP string intrinsics we describe are: Echo,print,strlen,trim,ltrim,rtrim,substr,strtolower,strtoupper,str_replace.

Echo and Print
See the difference between PHP Echo and PHP echo and print.

Strlen
The strlen function can get the length of a string. In the following example, the resulting variable $a has a length of 8.

$a = ' abcdef '; echo strlen ($a); 8

Trim
The function of the trim function is to remove whitespace from both sides of the string. For example, the value of the variable $a in the examples below is ' abcdef ', there is a space on each side of the string, and after trimming, the length of the string is 6, because two spaces are removed from both sides of the string.

$a = ' abcdef '; Echo strlen (Trim ($a)); 6

LTrim
The function of the LTrim function is to remove the space to the left of the string.

Echo ' nice ', ' try '; Nice Tryecho ' Nice ', LTrim (' Try '); Nicetry

RTrim
The function of the RTrim function is to remove the space to the right of the string.

Echo ' A ', ' B '; A becho rtrim (' a '), ' B '; Ab

Substr
A part of the string can be obtained through the SUBSTR function. The SUBSTR function syntax is as follows:

SUBSTR (String,start,length)

This means starting from the start position of the string, and intercepting the length of the string. The position of the first character of the strings string is 0, not 1. Examples are as follows:

echo substr (' blablar.com ', 0, 3); Bla


The above example indicates that starting with the first character of the string, 3 characters are intercepted, and the result is a bla.

echo substr (' blablar.com ', 3,5); Blar.

The above example means starting from the fourth character of the string blablar.com, capturing 5 characters, and getting the result is blar.

You can also not write the parameter length, which means that all subsequent strings are intercepted from the start position, for example:

echo substr (' blablar.com ', 3); Blar.com


Strtolower
The function of Strtolower is to turn the string all lowercase. Examples are as follows:

echo strtolower (' blablar.com ');//blablar.com

Strtoupper
Strtoupper and Strtolower instead, the function is to capitalize the string. Examples are as follows:

Echo strtoupper (' China '); China

Str_replace
The role of Str_replace is to replace strings. The syntax for the Str_replace function is as follows:

Str_replace (Search,replace,subject)

In the subject string, find any string that matches search, and replace all search strings with replace.

Examples are as follows:

echo str_replace ("Bla", "CHA", "blablar"); Chachar

In the example above, with CHA instead of all the BLA in the Blablar string, the returned result is Chachar.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.