php ignore special characters,PHP忽略第5个字符?(PHP ignore 5th character?)

PHP忽略第5个字符?(PHP ignore 5th character?)

我有一个简单的PHP问题。

在我的PHP中 ,我有这个:

$variable = 'howareyou';

有可能以某种方式修改代码,因此它只计算变量的第6个字符?

所以之后,当回声

它会说怎么代替你怎么样 。

我需要用数字过滤它,比如5号或6号 。 这可能吗?

谢谢!

I have a simple php question.

in my php, I have this:

$variable = 'howareyou';

is it possible to somehow modify the code so it only counts up to 6th character of the variable?

so after, when echo'd

it would say howare instead of howareyou.

I need to filter it with a number, like 5th or 6th. Is this possible?

Thanks!

原文:https://stackoverflow.com/questions/5321237

更新时间:2019-12-05 11:40

最满意答案

只需使用子串。

$variable = substr($variable, 0, 6)

语法是substr(string,start,length),并记住它们是零索引的。

Just use substring.

$variable = substr($variable, 0, 6)

The syntax is substr(string, start, length) and remember that these are zero indexed.

2011-03-16

相关问答

它错过了最后一个值,因为如果找到+则只添加数字。 所以对于最后一个unparsedNumber你永远不会输入else if (c == '+')块。 让我建议一个更紧凑的解决方案: public int addFromString(string str)

{

string trimmed = str.Trim();

if (str.StartsWith("+") || str.EndsWith("+")) return 0; // invalid -> return immedia

...

你可以做line = line[1:]去掉第一个字符。 但是,如果你这样做,你应该添加一个断言,第一个字符确实是一个选项卡,以避免在没有领先选项卡的情况下损坏数据。 有一个更容易的选择,也可以处理其他几种情况,如果要删除的东西不存在,则不会中断任何事情。 您可以使用line = line.strip()所有前导和尾随空白。 或者,使用.lstrip()仅剥离前导空白,如果要保留其他空白并只删除制表符,则可以将'\t'作为参数添加到任一方法调用中。 You could do line = line[

...

只需使用子串。 $variable = substr($variable, 0, 6)

语法是substr(string,start,length),并记住它们是零索引的。 Just use substring. $variable = substr($variable, 0, 6)

The syntax is substr(string, start, length) and remember that these are zero indexed.

^(?!(.)\1+$)[XO]{5}$

试试看。 https://regex101.com/r/uK9cD8/1 ^(?!(.)\1+$)[XO]{5}$

Try this.See demo. https://regex101.com/r/uK9cD8/1

这将做到这一点... ^\w\d\.(\d{3})\s(?:([YN])|\d)\s*(\d:\d{2})$

我对你的正则表达式做了一些其他的改变,因为我更容易根据你的数据重写它,然后尝试修改你的数据。 这将捕获Y或N,否则它不会捕获该组中的任何内容。 我也试图用你的持续时间正则表达式更具体。 更新:这适用于您的新需求... ^\w\d\.(\w{3})\s(?:([YN])|\d|\s)\s*(\d:\d{2})$

你可以在这里看到它处理你的数据... http://regexr.com?3

...

你能尝试使用str_replace吗? $datetogoto = $_GET['datetogoto'];

$datetogoto = str_replace("-","", $datetogoto);

参考: http : //us1.php.net/str_replace 或者,如果你想获得在查询字符串中发送的任何日期格式,那么使用urlencode() header('Location: ../index.php?newsdate='.urlencode($datetogoto));

...

\字符是一个转义字符。 你得到一个语法错误,因为\"是在String文字中放置一个"字符的转义序列。 要在String文本中放置\ ,您需要使用\\ (第一个\转义第二个\的特殊含义)。 所以一个语法正确的陈述是: String[] parts = "\u0041\u006e\u0064\u0072\u006f\u0069\u0064".split("\\");

但是这不会给你想要的,因为第一个参数不包含任何\字符。 (另外, split()方法需要一个正则表达式,而\不是一个有效的正则表达式

...

您可以在for循环中使用范围功能: text = "Do you like green eggs and ham? I like them, Sam I am! Do you like green eggs and ham, Sam? Do you like them, Sam I am?"

transform = ''

for l in range(4, len(text), 5):

transform += text[l]

#remove spaces (as your examp

...

由于 - 字符串的长度为3或4,因此在-之后只能获取3个字符。 这是代码片段 LEFT(@a, CHARINDEX('-', @a) + 3) = LEFT(@b, CHARINDEX('-', @b) + 3)

Since the length of sub-string after - is either 3 or 4, you just only fetch 3 characters after -. Here is code snippet LEFT(@a, CHARINDEX('-',

...

使用此基于交替的正则表达式: \.:\d{3}|:(\d{3})

并抓住你所在比赛的第一组。 RegEx演示 Use this alternation based regex: \.:\d{3}|:(\d{3})

And grab captured group #1 for your matches. RegEx Demo

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值