Home  >  Article  >  Backend Development  >  PHP gets the content between specified characters implementation code_PHP tutorial

PHP gets the content between specified characters implementation code_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 16:56:271004browse

php implementation code for obtaining the content between specified characters. This is a substring between two substrings in a string. For example, to obtain the hzhuti substring from the string www.hzhuti.com, let this PHP function Let’s implement it,

the code is as follows:

 代码如下 复制代码
function get_between($input, $start, $end) { 
 
  $substr = substr($input, strlen($start)+strpos($input, $start),
 
 (strlen($input) - strpos($input, $end))*(-1)); 
 
  return $substr; 
 

 
$string = "www.hzhuti.com"; 
 
$start = "www."; 
 
$end = ".com"; 
 
echo get_between($string, $start, $end);  // output:hzhuti

However, this function has a limitation, that is, the $start substring and $end substring can only appear once in the entire string. Please see the example below:

 代码如下 复制代码
$string = "http://www.hzhuti.com/"; 
 
$start = "http://"; 
 
$end = "/";

Obviously I want to get the domain name part of this standard URL. Since the $end substring is not unique in the entire string, problems will occur. Please be careful when using it!

Example

as
NAS/NMS COMPSITE (NasdaqSC:^IXIC) Quote data by ReutersIndex Value:2,030.08Trade Time:5:16PM ETChange: 35.40 (1.71%)Prev Close:2,065.48Open:2,072.95Day 's Range:2,026. 20 - 2,073.4252wk Range:1,359.32 - 2,153.831d

For such a string, I want to intercept the data 2,030.08 between Index Value: and Trade Time:. How to intercept it? I want a universal method

Code

The code is as follows
 代码如下 复制代码

$str= "NAS/NMS COMPSITE (NasdaqSC:^IXIC) Quote data by ReutersIndex Value:2,030.08Trade Time:5:16PM ETChange: 35.40 (1.71%)Prev Close:2,065.48Open:2,072.95Day 's Range:2,026.20 - 2,073.4252wk Range:1,359.32 - 2,153.831d ";
preg_match( " 'Index Value:(.+)Trade Time 's ",$str,$arr);
if($arr){
echo $arr[ "1 "];
}
?>

Copy code
$str= "NAS/NMS COMPSITE (NasdaqSC:^IXIC) Quote data by ReutersIndex Value:2,030.08Trade Time:5:16PM ETChange: 35.40 (1.71%)Prev Close:2,065.48Open:2,072.95Day Range:2,0 26.20 - 2,073.4252wk Range:1,359.32 - 2,153.831d ";

preg_match( " 'Index Value:(.+)Trade Time 's ",$str,$arr);

if($arr){ } ?> http://www.bkjia.com/PHPjc/631594.html
www.bkjia.com
truehttp: //www.bkjia.com/PHPjc/631594.htmlTechArticlephp code to get the content between specified characters. This is a code to get the substring between two substrings in a string. String, if you get the hzhuti substring from the string www.hzhuti.com, let this PHP function...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn