这篇文章主要介绍了php查找字符串出现次数的方法,较为详细的介绍了substr_count函数的用法,并补充说明了相关的字符串操作函数,具有一定的实用价值,需要的朋友可
本文实例讲述了php查找字符串出现次数的方法。分享给大家供大家参考。具体方法如下:
在php中查找字符串出现次数的查找可以通过substr_count()函数来实现,下面就来给大家详细介绍这些函数.
substr_count($haystack, $needle [,$offset [,$length]])
其中参数:
$haystack表示母字符串,,$needl表示要查找的字符
$offset表示查找的起点,$length表示查找的长度,均为可选参数
实例代码如下:
复制代码 代码如下:
$str="this is a test";
echo substr_count($str,'is') .'
';
echo substr_count($str,'is',3) .'
';
echo substr_count($str,'is',3,3) .'
';
?>
实例代码如下:
复制代码 代码如下:
$str = 'http://www.jb51.net 脚本之家_脚本编程入门教程_脚本设计交流_字符出现次数';
echo substr_count($str,'w').'
';
echo substr_count($str,'t').'
';
echo substr_count($str,'脚本之家');
?>
输出结果为:
3
3
1
再分享一些字符串查找函数:
strstr — 查找字符串的首次出现
stristr strstr不区分大小写的版本
strpos -查找字符串首次出现的位置
string substr ( string $string , int $start [, int $length ] )
string strrchr ( string $haystack , mixed $needle )
strripos -计算指定字符串在目标字符串中最后一次出现的位置(不区分大小写)
stripos -查找字符串首次出现的位置(不区分大小定)
strrpos -计算指定字符串在目标字符串中最后一次出现的位置
希望本文所述对大家的PHP程序设计有所帮助。

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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Atom editor mac version download
The most popular open source editor

SublimeText3 Linux new version
SublimeText3 Linux latest version

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

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.

SublimeText3 English version
Recommended: Win version, supports code prompts!
