删除PHP中的字符后的所有内容

时间:2022-09-07 20:23:51

can any tell how to remove characters after ? in php. I have one string test?=new i need to remove the characters as well as = from that string.

可以告诉我如何删除字符后?在PHP中。我有一个字符串测试?= new我需要从该字符串中删除字符以及=。

8 个解决方案

#1


23  

Shortest one:

echo strtok('test?=new', '?');

If you want to keep the question mark, the solution is almost the same:

如果你想保留问号,解决方案几乎是一样的:

echo strtok('test?=new', '?').'?';

#2


3  

This solution uses a simple regular expression to remove the ? character and all characters following it.

这个解决方案使用简单的正则表达式来删除?字符及其后的所有字符。

$string = "test?p=new";
$new_string = preg_replace("/\?.+/", "", $string);

#3


2  

Why not:

$pos = strpos($str, '?'); // ? position
$str = substr($str, 0, $pos);

#4


2  

You can do this with a well-written regex, but the much simpler and quicker way to do it is to explode the string on the "?" character, and use the first element in the resulting array.

你可以用一个写得很好的正则表达式做到这一点,但更简单快捷的方法是在“?”上爆炸字符串。字符,并使用结果数组中的第一个元素。

$str = "test?=new";
$str2 = explode("?", $str);
$use_this = $str2[0];

$use_this[0] will be "test". If you want to add the "?" back, just concatenate:

$ use_this [0]将是“test”。如果要添加“?”回来,只是连接:

$use_this = $use_this."?";

#5


1  

You could always try using preg_replace() as well:

您也可以尝试使用preg_replace():

$string = 'test?q=new';
$result = preg_replace("/\?.+/", "", $string);

If, for some reason, you are wanting to keep the ? in the result... you could also do this:

如果由于某种原因,你想要保留?在结果中......你也可以这样做:

$string = 'test?q=new';
$result = preg_replace("/\?.+/", "?", $string);

(or, you could use a positive look-behind assertion, as @BlueJ774 suggested,) like this:

(或者,你可以使用正面的后视断言,如@ BlueJ774建议的那样),像这样:

$result = preg_replace("/(?<=\?).+/", "", $string);

But ideally, and for future reference, if you are working with a query string, you probably will want to use parse_str at some point, like this:

但理想情况下,为了将来参考,如果您正在使用查询字符串,您可能希望在某些时候使用parse_str,如下所示:

$string = 'test?q=new';
parse_str($string, $output);

Because that will give you an array ($output, in this case,) with which to work with all of the parts of the query string, like this:

因为这将为您提供一个数组(在本例中为$ output),可以使用该数组来处理查询字符串的所有部分,如下所示:

Array
(
    [test?q] => new
)

But normally... you would probably just want to be working with the query string by this point... so the output would be more like this:

但通常......你可能只想在这一点上使用查询字符串......所以输出更像是这样的:

Array
(
    [q] => new
)

#6


1  

Here is one-liner:

这是单行:

$s = strpos($s, '?') !== FALSE ? strtok($s, '?') : $s;

You can test it by the following command-line:

您可以通过以下命令行对其进行测试:

php -r '$s = "123?456"; $s = strpos($s, "?") !== FALSE ? strtok($s, "?") : $s; echo $s;'

#7


0  

substr and strpos

The simplest way to do this is with substr() DOCs and strpos() DOCs.

最简单的方法是使用substr()DOC和strpos()DOC。

$string = 'test?=new';
$cut_position = strpos($string, '?') + 1; // remove the +1 if you don't want the ? included
$string = substr($string, 0, $cut_position);

As you can see substr() extracts a sub-string from a string by index and strpos() returns the index of the first instance of the character it is searching for (in this case ?).

正如您所看到的,substr()通过索引从字符串中提取子字符串,而strpos()返回它正在搜索的字符的第一个实例的索引(在本例中为?)。

#8


0  

Use the strstr function.

使用strstr函数。

<?php
$myString = "test?=new";
$result = strstr($myString, '=', true);

echo $result ;

The third parameter true tells the function to return everything before the first occurrence of the second parameter.

第三个参数true告诉函数在第一次出现第二个参数之前返回所有内容。

#1


23  

Shortest one:

echo strtok('test?=new', '?');

If you want to keep the question mark, the solution is almost the same:

如果你想保留问号,解决方案几乎是一样的:

echo strtok('test?=new', '?').'?';

#2


3  

This solution uses a simple regular expression to remove the ? character and all characters following it.

这个解决方案使用简单的正则表达式来删除?字符及其后的所有字符。

$string = "test?p=new";
$new_string = preg_replace("/\?.+/", "", $string);

#3


2  

Why not:

$pos = strpos($str, '?'); // ? position
$str = substr($str, 0, $pos);

#4


2  

You can do this with a well-written regex, but the much simpler and quicker way to do it is to explode the string on the "?" character, and use the first element in the resulting array.

你可以用一个写得很好的正则表达式做到这一点,但更简单快捷的方法是在“?”上爆炸字符串。字符,并使用结果数组中的第一个元素。

$str = "test?=new";
$str2 = explode("?", $str);
$use_this = $str2[0];

$use_this[0] will be "test". If you want to add the "?" back, just concatenate:

$ use_this [0]将是“test”。如果要添加“?”回来,只是连接:

$use_this = $use_this."?";

#5


1  

You could always try using preg_replace() as well:

您也可以尝试使用preg_replace():

$string = 'test?q=new';
$result = preg_replace("/\?.+/", "", $string);

If, for some reason, you are wanting to keep the ? in the result... you could also do this:

如果由于某种原因,你想要保留?在结果中......你也可以这样做:

$string = 'test?q=new';
$result = preg_replace("/\?.+/", "?", $string);

(or, you could use a positive look-behind assertion, as @BlueJ774 suggested,) like this:

(或者,你可以使用正面的后视断言,如@ BlueJ774建议的那样),像这样:

$result = preg_replace("/(?<=\?).+/", "", $string);

But ideally, and for future reference, if you are working with a query string, you probably will want to use parse_str at some point, like this:

但理想情况下,为了将来参考,如果您正在使用查询字符串,您可能希望在某些时候使用parse_str,如下所示:

$string = 'test?q=new';
parse_str($string, $output);

Because that will give you an array ($output, in this case,) with which to work with all of the parts of the query string, like this:

因为这将为您提供一个数组(在本例中为$ output),可以使用该数组来处理查询字符串的所有部分,如下所示:

Array
(
    [test?q] => new
)

But normally... you would probably just want to be working with the query string by this point... so the output would be more like this:

但通常......你可能只想在这一点上使用查询字符串......所以输出更像是这样的:

Array
(
    [q] => new
)

#6


1  

Here is one-liner:

这是单行:

$s = strpos($s, '?') !== FALSE ? strtok($s, '?') : $s;

You can test it by the following command-line:

您可以通过以下命令行对其进行测试:

php -r '$s = "123?456"; $s = strpos($s, "?") !== FALSE ? strtok($s, "?") : $s; echo $s;'

#7


0  

substr and strpos

The simplest way to do this is with substr() DOCs and strpos() DOCs.

最简单的方法是使用substr()DOC和strpos()DOC。

$string = 'test?=new';
$cut_position = strpos($string, '?') + 1; // remove the +1 if you don't want the ? included
$string = substr($string, 0, $cut_position);

As you can see substr() extracts a sub-string from a string by index and strpos() returns the index of the first instance of the character it is searching for (in this case ?).

正如您所看到的,substr()通过索引从字符串中提取子字符串,而strpos()返回它正在搜索的字符的第一个实例的索引(在本例中为?)。

#8


0  

Use the strstr function.

使用strstr函数。

<?php
$myString = "test?=new";
$result = strstr($myString, '=', true);

echo $result ;

The third parameter true tells the function to return everything before the first occurrence of the second parameter.

第三个参数true告诉函数在第一次出现第二个参数之前返回所有内容。