PHP intercepts the specified part of a string by symbol

Source: Internet
Author: User
Tags explode

String interception is more commonly used in PHP development, and there are many requirements for interception, such as the operation of a URL link: http://www.baijunyao.com/index.php/Home/Index/article/aid/12

Sometimes we want to intercept the last slash '/' behind the number;

Sometimes we need to intercept the first slash '/' in front of the content to determine the user Input URL link without http:/, and so on;

There are many ways to intercept PHP's built-in functions in a string, simply write a few examples;

$str = ' 123/456/789/abc ';

You can do this by intercepting the contents of the first slash :

Echo substr ($str, 0,strpos ($str, '/'))

Or

$array =explode ('/', $str); Echo $array [0];//output 123

You can do this by intercepting the contents of the first slash :

Echo substr ($str, Strpos ($str, '/') +1);//Output 456/789/ABC


To intercept the contents of the last slash , you can do the following:

Echo Trim (STRRCHR ($str, '/'), '/');

Or if you know the number of slashes

$array =explode ('/', $str); Echo $array [3];//output ABC


But if you don't know how many slashes there are? What if you want the middle of a second slash and a third slash?

The following I wrote this function can be easily resolved as above all problems;

/** *  the specified portion of a string by symbol  *  @param  string  $str   string to intercept  *  @param   string  $sign   Symbols to be intercepted  *  @param  int  $number   If positive starts from left to right from 0 Negative numbers are cut from right to left  *  @return  string  return intercepted content  */function cut_str ($str, $sign, $number) {      $array =explode ($sign,  $str);     $length =count ($array);     if ($number <0) {         $new _array=array_reverse ($array);          $abs _number=abs ($number);         if ($abs _number> $length) {             return  ' ERROR ';        }else{             return  $new _array[$abs _number-1];         } &Nbsp;  }else{        if ($number >= $length) {             return  ' ERROR ';         }else{            return $ array[$number];        }    }}

Example:

Echo cut_str ($str, '/', 0); Output 123echo cut_str ($str, '/', 2); Output 789echo cut_str ($str, '/',-1);//Output Abcecho cut_str ($str, '/',-3);//Output 456

How to use this function to intercept the specified part of the content is strong enough;

650) this.width=650; "alt=" 1.jpg "src=" http://www.baijunyao.com/Upload/image/ueditor/20150504/1430670684582054. JPG "title=" 1430670684582054.jpg "/>


This article is from the "Bai Jun Remote Blog" blog, please be sure to keep this source http://shuaibai123.blog.51cto.com/10163353/1686248

PHP intercepts the specified part of a string by symbol

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.