这篇文章主要介绍了PHP获取一个字符串中间一部分字符的方法,即获取字符串中两个指定子字符串之间子字符串的方法,需要的朋友可以参考下
这是一个获取字符串中两个子串之间的子串,如从字符串中获取coderbolg子串,,就让这个PHP函数来实现吧,代码如下:
复制代码 代码如下:
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
不过这个函数有个局限,就是$start子串和$end子串在整个串中只能出现一次。请看下面的例子:
复制代码 代码如下:
$string = "http://www.jb51.net/";
$start = "http://";
$end = "/";
很明显我想获取这个标准URL的域名部分,由于$end子串在整个串中不是唯一的,所以就会出现问题,请使用时务必注意!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Dreamweaver CS6
Visual web development tools

WebStorm Mac version
Useful JavaScript development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

Notepad++7.3.1
Easy-to-use and free code editor