Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The usage of intercepting string function in php

2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "the use of intercepting string function in php". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn "the use of intercepting string functions in php".

Php comes with an intercept string function, can only deal with English, numbers can not intercept Chinese mixed, oh, friends in need can refer to, the latter is easier to use, the first is mainly for beginners to learn to use.

Php / / Construction string $str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; echo "original string:". $str. "; / / intercept $str1 = substr ($str,5) in various ways; echo" from the fifth character to the end: ". $str1."; $str2 = substr ($str,9,4); echo "take 4 characters from the 9th character:". $str2. "; $str3 = substr ($str,-5); echo" countdown 5 characters: ". $str3." $str4 = substr ($str,-8,4); echo "take 4 characters back from the penultimate character:". $str4. "; $str5 = substr ($str,-8,-2); echo" from the penultimate character to the penultimate character: ". $str5.";? >

Support mixed Chinese and English interception.

/ *

Parameters:

$str_cut needs to truncate the string

Maximum length of string display allowed by $length

Program function: intercept full-width and half-width (Chinese and English) mixed strings to avoid garbled code

, /

Function substr_cut ($str_cut,$length)

{

If (strlen ($str_cut) > $length)

{

For ($iTun0; $I

< $length; $i++) if (ord($str_cut[$i]) >

128) $iTunes +

$str_cut = substr ($str_cut,0,$i).

}

Return $str_cut

}

? >

1. The reversal function of the string $str='hello';//---- > ollehecho strrev ($str).''; / / this is a function provided by the system / write a function to reverse $strArr = str_split ($str); / / Array ([0] = > h [1] = > e [2] = > l [3] = > l [4] = > o) $str1='';for ($i=count ($strArr)-1; $I > = 0; $iMurt -) {$str1.=$strArr [$I] } echo $str1; 2. Strstr / get the suffix .jpg or jpg of the file can either $file='abc.exe.jpg'; echo strrchr ($file,'.').''; / / this is the system function used, and the file name echo strrev (strstr ($file),'.', true).'; / / strstr () returns the first occurrence of the string, plus true indicates that the return is. The previous string echo substr ($file,strrpos ($file,'.')).''; / / strrpos () returns the last time. Location, and then intercept $strArr=explode ('.', $file); echo $strArr [count ($strArr)-1].''; / / explode () function splits the string into an array / / echo array_pop ($strArr).'; / / echo end ($strArr).'; echo pathinfo ($file,PATHINFO_EXTENSION).'' The / / pathinfo () function returns information about the path of the file echo strrev (substr (strrev ($file), 0Gramsstrpos ($file,'.').''; 3.Unip / will be 1234567890-> 1234567890$ str='1234567890';$strArr=str_split (strrev ($str), 3); echo strrev (implode (',', $strArr)). / /-> 1234567890 so far, I believe you have a deeper understanding of "the use of intercepting string functions in php". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report