简体   繁体   English

PHP函数,用于删除子字符串之前的所有内容(带有包含选项)

[英]PHP function for removing everything before a substring (with including option)

I need help making a php function. 我需要帮助使php函数。 I want to create a function that removes everything before a specific word or character in a string. 我想创建一个函数,以删除字符串中特定单词或字符之前的所有内容。 I know how to do that part, but I am new to making a function out of it, and I need to add an option to it... for example. 我知道该怎么做,但是我不擅长使用它,例如,我需要为其添加一个选项。 If I have this string... 如果我有这个字符串...

$str="I like to eat cheese, crackers and ham";

And I want to remove everything before the word cheese I could use this... 我想删除所有单词“ cheese”之前可以使用的...

if(($pos=strpos($str,'cheese'))!== false) $str=substr($str,$pos);

and I'd end up with 我最终会

cheese, crackers and ham 奶酪,饼干和火腿

if I want to include the word cheese in the removal, I could use... 如果我想在移除字词中加入奶酪一词,可以使用...

if(($pos=strpos($str,'cheese'))!== false) $str=substr($str,$pos+6);

the +6 at the end is the length of the word "cheese". 末尾的+6是单词“奶酪”的长度。 I would get... 我会...

, crackers and ham ,饼干和火腿

I'd like to be able to use a function called remove_before to get my new string, like this... 我希望能够使用一个名为remove_before的函数来获取我的新字符串,如下所示...

$str=remove_before("cheese",$str,0);

The 3rd variable would indicate if you also want to remove the word you're looking for. 第三个变量将指示您是否还想删除要查找的单词。 A zero here would mean, remove everything before the word cheese and a 1 would mean remove the word cheese AND everything before it. 这里的零表示删除奶酪一词之前的所有内容,而数字1表示删除奶酪一词及其之前的所有内容。 Thank you. 谢谢。

What you're looking for is something like this: 您正在寻找的是这样的:

<?php
function remove_before($needle, $haystack, $removeNeedle=false) {

    $pos = strpos($haystack, $needle);

    if (($pos !== false)) {
        return substr($haystack, $pos + (strlen($needle) * $removeNeedle));
    }

    return $haystack; // if word not found, return full string.
}

$needle = "cheese";
$haystack = "I like to eat cheese, crackers and ham";

echo remove_before($needle, $haystack, false);
?>

$pos + (strlen($needle)*$removeNeedle) $removeNeedle is a boolean, meaning its value is either 1 or 0. if you multiply by 1, then value is value, if you multiply by 0, value is 0. so basically, you multiply the length of needle by 1 or 0. $pos + (strlen($needle)*$removeNeedle) $ removeNeedle是一个布尔值,表示其值为1或0。如果乘以1,则值为value;如果乘以0,则值为0。基本上,您将针的长度乘以1或0。

The boolean at the end, is optional, as its default value is false. 最后的布尔值是可选的,因为其默认值为false。

The code is written by me, and is free of use to anyone, without limit. 该代码由我编写,任何人都可以免费使用,没有限制。

<?php
$str = "I like to eat cheese, crackers and ham";

function remove_before($needle, $haystack, $include = false)
{
    $pos = strpos($haystack, $needle);
    if ($pos !== false) {
        if (!$include) {
            $pos += strlen($needle);
        }
        return substr($haystack, $pos, strlen($haystack));
    }
    return null;
}

echo remove_before('cheese', $str, true);
echo remove_before('cheese', $str);

> cheese, crackers and ham
> , crackers and ham

Take a look at the documentation for creating functions http://php.net/manual/en/functions.user-defined.php 看看用于创建函数的文档http://php.net/manual/zh/functions.user-defined.php

But also look at the documentation for passing arguments to functions http://php.net/manual/en/functions.arguments.php 另外,请参阅将参数传递给函数的文档http://php.net/manual/zh/functions.arguments.php

What you want to pay attention to in the second link is passing default arguments. 您要在第二个链接中注意的是传递默认参数。 This allows for you to set a default value for an $arg that when it isn't set manually, defaults to the defined value. 这允许您为$ arg设置默认值,如果不手动设置它,则默认为已定义的值。 This allows for PHP to mock the function overloading of other languages. 这允许PHP模拟其他语言的函数重载。

Also it is important to do some sort of error checking inside of your function, depending on the defaults set, where my example just assumes that the user will pass a boolean value. 同样重要的是,在函数内部进行某种类型的错误检查,具体取决于默认设置,我的示例仅假设用户将传递布尔值。

function remove_before ($needle, $haystack, $isNotIncluded) {
  if(($pos=strpos($haystack,$needle))!== false) 
    $haystack=substr($haystack,$isNotIncluded?$pos + strlen ($needle) :$pos);
  return $haystack;
}

//sorry, i'm typing on Galaxy s6, so I'm still not check it. //很抱歉,我在Galaxy s6上打字,所以我仍然没有检查它。 Please try and double check substr, strlen... I just give you the basic function synstax. 请尝试仔细检查substr,strlen ...我只是给您基本功能语法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM