php PHP如何检查第一个字符是否为£,如果是则将其删除
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7956655/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP How to check if the first character is a £ and remove it if it is
提问by user794846
How exactly do I check of the first character in $value
is £ and then remove it if is?
我究竟如何检查$value
是 £中的第一个字符,然后将其删除?
Even better would be to check if it is not a number and then remove it.
更好的是检查它是否不是数字,然后将其删除。
My current code:
我目前的代码:
echo '<dl style="margin-bottom: 1em;">';
foreach ($row as $key => $value)
{
echo "<dt>$key</dt><dd>$value</dd>";
}
echo '</dl>';
回答by Hippyjim
Some folks are suggesting trim - which is probably fine for most uses, but might also remove a trailing '£' as well.
有些人建议修剪 - 这可能适用于大多数用途,但也可能删除尾随的“£”。
If you really only want to remove a '£' from the start of the string, you might want to look at using ltrim
如果你真的只想从字符串的开头删除一个 '£',你可能想看看使用ltrim
e.g.
例如
$value = ltrim($value, '£');
(I'm aware the intent of the question is about stripping non-numerics from a string, but I found this while Googling for removing a character only from the start, so no doubt others will as well. This should help them out)
(我知道这个问题的意图是从字符串中剥离非数字,但我在谷歌搜索时发现这一点只从一开始就删除了一个字符,所以毫无疑问其他人也会这样做。这应该对他们有所帮助)
回答by qeek
http://php.net/manual/en/function.str-replace.php
http://php.net/manual/en/function.str-replace.php
str_replace('£', '', $value);
or of course
或者当然
ltrim($value, '£')
if you only want to get rid of the first character
如果你只想摆脱第一个字符
Also: http://php.net/manual/en/function.is-numeric.php
另外:http: //php.net/manual/en/function.is-numeric.php
Update: Check Antony's answerbelow. It has a more precise solution and should be the accepted answer.
更新:检查下面安东尼的回答。它有一个更精确的解决方案,应该是公认的答案。
回答by Marcx
You can do this:
你可以这样做:
if (substr($value,0,1)=="£") $value = substr($value,1);
or
或者
if ($value[0]=="£") $value = substr($value,1);
or (this way you check if the character (substracted from value) is numeric or not)..
或(通过这种方式检查字符(从值中减去)是否为数字)。
if (!is_numeric(substr($value,0,1))) $value = substr($value,1);
回答by Naftali aka Neal
回答by Marc B
if ($values[0] == '£') { then
$values = substr($values, 1); // grab char 1 onwards, removing char 0/£
}
This uses the "treat string as an array" shortcut notation, and boils down to
这使用“将字符串视为数组”快捷表示法,并归结为
if (substr($values, 0, 1) == '£') {
Note that this will fail on multi-byte character sets, and you'd have to use mb_substr()
instead, which is the multibyte-aware version.
请注意,这将在多字节字符集上失败,您必须mb_substr()
改用,这是多字节感知版本。
回答by Oldskool
You can subtract the first character and perform a is_int() check on that, like so:
您可以减去第一个字符并对其执行 is_int() 检查,如下所示:
$firstChar = substr($yourString, 0, 1);
if(!is_int($firstChar)) {
// Not an integer, strip the first character altogether
$newString = substr($yourString, 1);
}
Then, $newString will hold the content, without the first character (if that was not an integer).
然后, $newString 将保存内容,没有第一个字符(如果它不是整数)。
回答by hair raisin
$value = preg_replace('/^\D*/', '', $value);
This will remove all the non-digits from the beginning of $value
这将删除开头的所有非数字 $value
回答by Antony D'Andrea
This might be useful for future visitors.
这可能对未来的访问者有用。
The question is removing the firstcharacter if it is a "£".
问题是删除第一个字符,如果它是“£”。
Of course this could be any character so the issue with the suggest ltrim
is that it will remove allof the consecutive characters, not just the first, e.g.
当然,这可以是任何字符,因此建议的问题ltrim
在于它将删除所有连续字符,而不仅仅是第一个字符,例如
ltrim('££10', '£');
Would output 10
not £10
. In this case, this might be acceptable but not for every case as we only want the first character.
将输出10
没有£10
。在这种情况下,这可能是可以接受的,但并非适用于所有情况,因为我们只想要第一个字符。
preg_replace('/^'.$char.'/', '', $input);
Will do this. $char
is the character that you want to check (in this case, '£').
会这样做。$char
是您要检查的字符(在本例中为“£”)。
The regex, /^£/
, would look for exactly one instance of £ anchored to the start of the string.
正则表达式 ,/^£/
将查找 £ 的一个实例,该实例锚定到字符串的开头。