加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

PHP删除搜索字符串旁边的单词

发布时间:2020-12-13 16:19:15 所属栏目:PHP教程 来源:网络整理
导读:我需要删除搜索字符串的下一个单词..我有像数组一样的搜索数组(‘aa’,’bb’,’é’); 这是我的段落’你好,这是测试段aa 123测试bb 456′. 在本段中,我需要删除123和456. $pattern = "/béb/i";$check_string = preg_match($pattern,'Hello,this is a tes
我需要删除搜索字符串的下一个单词..我有像数组一样的搜索数组(‘aa’,’bb’,’é’);

这是我的段落’你好,这是测试段aa 123测试bb 456′.

在本段中,我需要删除123和456.

$pattern        = "/béb/i";
$check_string       = preg_match($pattern,'Hello,this is a test paragraph aa 123 test é 456');

如何获得下一个字?请帮忙.

解决方法

这是我的解决方案:

<?php

//Initialization
$search = array('aa','bb','é');
$string = "Hello,this is a test paragraph aa 123 test bb 456";

//This will form (aa|bb|é),for the regex pattern
$search_string = "(".implode("|",$search).")";

//Replace "<any_search_word> <the_word_after_that>" with "<any_search_word>"
$string = preg_replace("/$search_strings+(S+)/","$1",$string);

var_dump($string);

将“SEARCH_WORD NEXT_WORD”替换为“SEARCH_WORD”,从而消除“NEXT_WORD”.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读