This article describes how to obtain a substring between two specified substrings in a string in PHP. For more information, see
This article describes how to obtain a substring between two specified substrings in a string in PHP. For more information, see
This is a substring used to obtain the substring between two substrings in a string. For example, to obtain the coderbolg substring from the substring, let this PHP function be implemented. The Code is as follows:
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.jb51.net ";
$ Start = "www .";
$ End = ". net ";
Echo get_between ($ string, $ start, $ end); // output: coderbolg
However, this function has a limitation that the $ start and $ end substrings can only appear once in the entire string. See the following example:
The Code is as follows:
$ String = "http://www.jb51.net /";
$ Start = "http ://";
$ End = "/";
Obviously, I want to obtain the domain name part of this Standard URL. Because the $ end substring is not unique in the entire string, this will cause a problem. Please pay attention when using it!