PHP之mb_strstr使用

本文详细介绍了 PHP 中的 mb_strstr 函数,该函数用于查找一个字符串在另一个字符串中的首次出现,并返回相应的子串部分。文章通过实例展示了如何使用此函数来获取不同部分的字符串。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

mb_strstr

  • (PHP 5 >= 5.2.0, PHP 7)
  • mb_strstr — Finds first occurrence of a string within another
  • 查找字符串在另一个字符串里的首次出现

Description

string mb_strstr ( 
    string $haystack , 
    string $needle [, 
    bool $before_needle = false [, 
    string $encoding =mb_internal_encoding() ]] 
    )

//mb_strstr() finds the first occurrence of needle in haystack and returns the portion of haystack. If needle is not found, it returns FALSE.
//mb_strstr() 查找了 needle 在 haystack 中首次的出现并返回 haystack 的一部分。 如果 needle 没有找到,它将返回 FALSE。

Parameters

haystack

  • The string from which to get the first occurrence of needle
  • 要获取 needle 首次出现的字符串。

needle

  • The string to find in haystack
  • 在 haystack 中查找这个字符串。

before_needle

  • Determines which portion of haystack this function returns. If set to TRUE, it returns all of haystack from the beginning to the first occurrence of needle (excluding needle). If set to FALSE, it returns all of haystack from the first occurrence of needle to the end (including needle).
  • 决定这个函数返回 haystack 的哪一部分。 如果设置为 TRUE,它返回 haystack 中从开始到 needle 出现位置的所有字符(不包括 needle)。 如果设置为 FALSE,它返回 haystack 中 needle 出现位置到最后的所有字符(包括了 needle)。

encoding

  • Character encoding name to use. If it is omitted, internal character encoding is used.
  • 要使用的字符编码名称。 如果省略该参数,将使用内部字符编码。

Return Values

  • Returns the portion of haystack, or FALSE if needle is not found.
  • 返回 haystack 的一部分,或者 needle 没找到则返回 FALSE。

Examples

<?php
/**
 * Created by PhpStorm.
 * User: zhangrongxiang
 * Date: 2018/2/1
 * Time: 下午10:27
 */

//*  * If set to true, it returns all of haystack from the beginning to the first occurrence of needle.
$strstr = mb_strstr( "hello china", "ll", true );
echo $strstr . PHP_EOL; //he

//* If set to false, it returns all of haystack from the first occurrence of needle to the end,
$strstr = mb_strstr( "hello china", "ll", false );
echo $strstr . PHP_EOL;//llo china

//hello china
echo mb_strstr( "hello china", "ll", true ) . mb_strstr( "hello china", "ll", false ) . PHP_EOL;


$strstr = mb_strstr( "hello China,hello PHP", "ll", true );
echo $strstr . PHP_EOL; //he

$strstr = mb_strstr( "hello China,hello PHP", "ll", false );
echo $strstr . PHP_EOL; //llo China,hello PHP

$strstr = mb_strstr( "PHP是世界上最好的语言?", "最好", true );
echo $strstr.PHP_EOL; //PHP是世界上
$strstr = mb_strstr( "PHP是世界上最好的语言?", "最好", false );
echo $strstr.PHP_EOL; //最好的语言?

文章参考

http://php.net/manual/en/function.mb-strstr.php

转载注明出处

转载于:https://www.cnblogs.com/zhangrxiang/p/8401631.html

<think>嗯,用户想找关于如何结合使用switch和strstr的代码示例。我需要先回顾一下这两个函数的基本用法,然后想想它们可以在什么场景下一起使用。首先,switch语句在C语言中用于多分支选择,通常根据不同的常量表达式执行不同的代码块。而strstr函数是用来在字符串中查找子串的位置的,返回第一次出现的指针。用户可能需要根据strstr的查找结果来触发不同的case分支,或者反过来,在switch的某个case中使用strstr进行字符串处理。 但直接结合可能有难度,因为switch的条件必须是一个整型表达式,而strstr返回的是指针。所以可能的做法是,先使用strstr查找字符串,根据结果生成一个整型值,再作为switch的判断条件。例如,如果找到特定子串,设置一个标志变量,然后在switch中使用这个变量。 比如,用户可能想根据不同的子串存在与否来执行不同的操作。比如,在文本处理中,查找关键字,然后根据关键字执行不同的命令。这种情况下,可以先用strstr检查是否存在某个关键字,然后根据结果设置不同的case值,或者直接在switch的case中处理不同的情况。另外,引用中的例子显示,switch可以用于处理菜单选择,比如引用[2]中的例子,用户输入数字选择功能。可能用户想结合字符串处理的功能,比如用strstr解析输入的命令字符串,然后根据匹配的子串触发不同的case。不过,这时候需要将字符串匹配的结果转换为switch可以处理的整型常量,比如枚举或者宏定义。或者,在某个case中,使用strstr来处理字符串。例如,在switch的一个case里,调用strstr来进一步处理字符串,比如引用[3]中的例子,翻转模式串后用strstr查找。这种情况下,switch可能用于选择不同的处理模式,然后在每个case里执行相应的字符串操作。 需要确保示例代码正确,比如在switch中使用strstr的结果,可能需要将指针转换为bool或其他整型值。或者,在case中使用常量表达式对应不同的字符串存在情况。但C语言的case标签必须是常量,所以不能直接用字符串内容作为case的值,只能通过预处理将字符串匹配结果映射到不同的整型常量。比如,可以这样设计:先用strstr检查多个可能的子串,根据找到哪一个来设置一个枚举变量,然后在switch中根据该枚举值执行不同的代码。例如:char *input= "openfile.txt"; if(strstr(input, "open")){cmd =CMD_OPEN;} elseif (strstr(input,"save")) {cmd= CMD_SAVE; }switch (cmd){case CMD_OPEN:...break;case CMD_SAVE: ...break;} 这样,strstr用于确定命令类型,然后switch根据命令类型执行相应的操作。这种情况下,两者的结合使用就比较自然。另外,用户提供的引用[3]中的例子,在翻转模式串后使用strstr进行匹配,可能可以结合switch来处理不同的匹配情况,比如根据匹配的位置执行不同的操作,但实际代码中可能没有这样做。不过,可以构造一个示例,比如在某个case中调用strstr,或者在switch外使用strstr的结果来决定进入哪个case。 综上,可能的代码示例包括: 1.使用strstr判断输入字符串中的关键字,转换为枚举或整型常量,再在switch中处理。2.在switch的某个case中,使用strstr进一步处理字符串,例如解析不同的子命令。 需要确保代码正确,符合C语法,并正确引用用户提供的引用中的例子,比如引用[3]中的strstr用法,引用[2]中的switch结构。现在,我需要整理这些思路,给出几个具体的示例,并说明如何结合使用这两个函数。同时,生成相关问题,帮助用户进一步学习。</think>### 结合使用 `switch` 与 `strstr` 的代码示例 #### 示例1:根据字符串内容触发 `switch` 分支 通过 `strstr` 检测字符串中是否包含特定关键字,将结果映射为整型常量,再通过 `switch` 执行不同操作。 ```c #include <stdio.h> #include <string.h> // 定义命令类型常量 enum Command { CMD_OPEN, CMD_SAVE, CMD_EXIT, CMD_UNKNOWN }; int main() { char input[] = "open document.txt"; enum Command cmd = CMD_UNKNOWN; // 使用 strstr 检测关键字 if (strstr(input, "open") != NULL) { cmd = CMD_OPEN; } else if (strstr(input, "save") != NULL) { cmd = CMD_SAVE; } else if (strstr(input, "exit") != NULL) { cmd = CMD_EXIT; } // 根据命令类型执行操作 switch (cmd) { case CMD_OPEN: printf("执行打开文件操作\n"); break; case CMD_SAVE: printf("执行保存文件操作\n"); break; case CMD_EXIT: printf("退出程序\n"); break; default: printf("未知指令\n"); } return 0; } ``` #### 示例2:在 `switch` 分支中使用 `strstr` 在 `switch` 的某个分支中,进一步使用 `strstr` 处理子任务(例如解析具体参数)[^3]。 ```c #include <stdio.h> #include <string.h> int main() { char command[] = "process:data=123"; int operation = 1; // 假设通过其他逻辑获取操作类型 switch (operation) { case 1: // 处理数据解析 char *data_ptr = strstr(command, "data="); if (data_ptr != NULL) { int value = atoi(data_ptr + 5); // 提取数值 printf("解析到数据: %d\n", value); } break; default: printf("未支持的操作\n"); } return 0; } ``` --- ### 结合逻辑说明 1. **`strstr` 的作用**:用于在字符串中查找子串,返回第一次出现的位置指针(若未找到返回 `NULL`)[^3]。 2. **`switch` 的限制**:`case` 标签必须是常量表达式(如整数或枚举值),无法直接处理字符串比较。因此需先将字符串逻辑转换为整型常量[^1]。 3. **典型场景**: - 命令行工具中解析用户输入(如检测 `"open"`、`"save"` 等关键词)。 - 文本处理时根据子串内容选择处理逻辑[^3]。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值