Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/249.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在PHP中从右边修剪字符串?_Php_String_Trim - Fatal编程技术网

如何在PHP中从右边修剪字符串?

如何在PHP中从右边修剪字符串?,php,string,trim,Php,String,Trim,我有一个字符串示例 this-is-the-example/exa 我想从上一行修剪/exa $string1 = "this-is-the-example/exa"; $string2 = "/exa"; 我使用的是rtrim($string1,$sting2) 但是输出是这是示例 我想这是作为输出的示例 这两个字符串都是动态的,并且可能在字符串中多次出现。但我只想删除最后一部分。此外,string2中不必包含/。这也可能是普通字符串。就像a,abc一样。您可以使用explode <

我有一个字符串示例

this-is-the-example/exa
我想从上一行修剪/exa

$string1 = "this-is-the-example/exa";
$string2 = "/exa";
我使用的是
rtrim($string1,$sting2)

但是输出是
这是示例

我想
这是作为输出的示例

这两个字符串都是动态的,并且可能在字符串中多次出现。但我只想删除最后一部分。此外,string2中不必包含
/
。这也可能是普通字符串。就像
a
abc
一样。

您可以使用explode

<?php

$x = "this-is-the-example/exa";

$y = explode('/', $x);

echo $y[0];
您可以使用explode

<?php

$x = "this-is-the-example/exa";

$y = explode('/', $x);

echo $y[0];

rtrim的第二个参数是字符掩码而不是字符串,最后的“e”被修剪,这是正常的

考虑使用其他东西,例如regexp(preg_replace)来满足您的需要

这会将所有内容保留在“/”字符之前:

$str = preg_replace('/^([^\/]*).*/','$1', 'this-is-the-example/exa');
这将删除最后一部分。

$str = preg_replace('/^(.*)\/.*$/','$1', 'this-is-the-example/exa/mple');

rtrim的第二个参数是字符掩码而不是字符串,最后的“e”被修剪,这是正常的

考虑使用其他东西,例如regexp(preg_replace)来满足您的需要

这会将所有内容保留在“/”字符之前:

$str = preg_replace('/^([^\/]*).*/','$1', 'this-is-the-example/exa');
这将删除最后一部分。

$str = preg_replace('/^(.*)\/.*$/','$1', 'this-is-the-example/exa/mple');

首先
explode
字符串,使用
array\u pop
从分解的数组中移除最后一个元素,然后
使用
/
再次将其内爆

$str = "this-is-the-example/exa";
if(strpos($str, '/') !== false)
{
    $arr = explode('/', $str);
    array_pop($arr);
    $str = implode('/', $arr);
    // output this-is-the-example
}
如果URL中有多个
/
,则此事件将起作用,并且仅删除最后一个元素

$str = "this-is-the-example/somevalue/exa";

if(strpos($str, '/') !== false)
{
    $arr = explode('/', $str);
    array_pop($arr);
    $str = implode('/', $arr);
    // output this-is-the-example
}

首先
explode
字符串,使用
array\u pop
从分解的数组中移除最后一个元素,然后
使用
/
再次将其内爆

$str = "this-is-the-example/exa";
if(strpos($str, '/') !== false)
{
    $arr = explode('/', $str);
    array_pop($arr);
    $str = implode('/', $arr);
    // output this-is-the-example
}
如果URL中有多个
/
,则此事件将起作用,并且仅删除最后一个元素

$str = "this-is-the-example/somevalue/exa";

if(strpos($str, '/') !== false)
{
    $arr = explode('/', $str);
    array_pop($arr);
    $str = implode('/', $arr);
    // output this-is-the-example
}

为了允许错误处理,如果在搜索字符串中找不到子字符串

<?php

$myString = 'this-is-the-example/exa';

//[Edit: see comment below] use strrpos, not strpos, to find the LAST occurrence 
$endPosition = strrpos($myString, '/exa');

// TodO; if endPosition === False then handle error, substring not found 

$leftPart = substr($myString, 0, $endPosition);

echo($leftPart);

?>

输出

这就是一个例子


为了允许错误处理,如果在搜索字符串中找不到子字符串

<?php

$myString = 'this-is-the-example/exa';

//[Edit: see comment below] use strrpos, not strpos, to find the LAST occurrence 
$endPosition = strrpos($myString, '/exa');

// TodO; if endPosition === False then handle error, substring not found 

$leftPart = substr($myString, 0, $endPosition);

echo($leftPart);

?>

输出

这就是一个例子

向…问好

向…问好


有多种方法可用于此:

使用
substr
():

使用regex():


有多种方法可用于此:

使用
substr
():

使用regex():

希望这有帮助。:)

只需尝试以下代码:

<?php
$this_example = substr("this-is-the-example/exa", 0, -4);  
echo "<br/>".$this_example; // returns "this-is-the-example"
?>

希望这有帮助。:)

只需尝试以下代码:

<?php
$this_example = substr("this-is-the-example/exa", 0, -4);  
echo "<br/>".$this_example; // returns "this-is-the-example"
?>



“字符串是动态的,并且可能在字符串中多次出现”-现在,如果存在另一个
/
您将不会剪切最后一部分我明白您的意思,我只是使用explode来获得所需的输出。通过此操作,我只能得到输出前的第一部分,如果替换字符串包含“a”这样的计划字符,“字符串是动态的,并且可能在字符串中多次出现”-现在,如果彼此之间存在
/
,则不会剪切最后一部分。我明白你的意思,我只是使用explode来获得所需的输出。通过此操作,我只能获得输出前/输出前的第一部分,如果替换字符串包含计划字符,例如“a”字符串可能有多个/或替换字符串也可能有普通abc。当$trim=“a”时,获取输出前的“这是ex”;字符串可能有多个/或替换字符串也可能有普通abc。当$trim=“a”时,将“this is the ex”作为输出获取;这两个字符串都是动态的,在少数情况下,替换项有时可能为空。也可以是普通的a、abc或任何东西。这两个字符串都是动态的,在少数情况下,替换项有时可能是空的。也可能是普通的a、abc或任何东西。当我将装饰框更改为“a”时,得到“这是ex”。我的错-如果您使用
builder()
而不是
strpos()
,它会起作用。搜索字符串“a”不是唯一的
strpos()
查找第一个匹配项,`strrpos()查找最后一个匹配项,这正是您所需要的。对不起;我已经更新了我的回答,当我将装饰盒更改为“a”时,我得到了“这是ex”。我的错误-如果您使用
builder()
而不是
strpos()
,它会工作的。搜索字符串“a”不是唯一的
strpos()
查找第一个匹配项,`strrpos()查找最后一个匹配项,这正是您所需要的。对不起;我已经更新了我的答案。我不一定要从/的右边修剪字符串。修剪字符串也可能有简单字符。在少数情况下,trim case也可能是空的。我不一定要从/开始修剪字符串。修剪字符串也可能有简单字符。在少数情况下,阀内件箱也可能是空的。