Home > Article > Backend Development > PHP gets the content between specified characters implementation code_PHP tutorial
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
|
Copy code | ||||
preg_match( " 'Index Value:(.+)Trade Time 's ",$str,$arr);