Implementation of PHP Intercepting Specified Part of String by Symbol

  • 2021-11-02 00:02:06
  • OfStack

String interception is commonly used in php development.

And there are many demands for interception.

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

Sometimes we need to intercept the content in front of the first slash '/' to judge whether the url link entered by the user has http://and so on;

There are many built-in functions for string interception php;

A simple example is as follows;

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

Intercepting the content before the first slash can be done like this:

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

Or


 $array=explode('/', $str);
 echo $array[0];
 //  Output  123

Intercepting the content after the first slash can be done as follows:

echo trim(strrchr($str, '/'),'/');

If you know the number of slashes


 $array=explode('/', $str);
 echo $array[3];
 // Output  abc

What if you don't know how many slashes there are? What if you want the content between the second slash and the third slash?

The following function can easily solve all the above problems;


 /**
 *  Intercepts the specified part of a string by symbol 
 * @param string $str  String to be intercepted 
 * @param string $sign  Symbols to be intercepted 
 * @param int $number  If it is a positive number, 0 Cut from left to right as the starting point   Negative numbers are truncated from right to left 
 * @return string  Returns the 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];
   }
  }else{
   if($number>=$length){
    return 'error';
   }else{
    return $array[$number];
   }
  }
 }

Example


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

Add: Let's look at php intercepting a string between two characters


/**
 * php Intercepts a string between specified characters. The default character set is utf-8 Power by  Big Ear Tutu 
 * @param string $begin  Start string 
 * @param string $end  End string 
 * @param string $str  String to be intercepted 
 * @return string
 */
function cut($begin,$end,$str){
 $b = mb_strpos($str,$begin) + mb_strlen($begin);
 $e = mb_strpos($str,$end) - $b;

 return mb_substr($str,$b,$e);
}

Call

echo $this->cut('token/','?code',$redirectUrl);die;

The above method is not very easy to use, when the interception is a string of values, there will be interception of the situation with the following bar


/*
 * php Intercepts a string between specified two characters 
 * */
function get_between($input, $start, $end) {
 $substr = substr($input, strlen($start)+strpos($input, $start),(strlen($input) - strpos($input, $end))*(-1));
 return $substr;
}

Call

$sVid = $this->get_between($redirectUrl, "token/", "?code=");

Summarize


Related articles: