我想在php中使用字符串中的子字符串,其中包含起始位置和最后位置而不是长度。 因为我们在php substr($ str,$ start_position,$ length)中有函数; 但我想要$ last_postion而不是$ length,因为我不知道起始位置后的长度,因为它是可变的。 例如$ str = 2012年10月8日 $ str = 2012年2月2日
答案 0 :(得分:0)
尝试使用:
substr($str,$start_position,$last_postion-$start_position);
答案 1 :(得分:0)
substr($str, $start_position, $last_postion-$start_position)
答案 2 :(得分:0)
如果你不知道确切的长度,你需要先使用strpos。 http://php.net/manual/en/function.strpos.php
你使用strpos($ the_string,$ the_string_to_find)找到你正在寻找的字符,然后在$ length中使用返回值作为substr。
但正如@john所说,如果你试图操纵一个日期,或者从日期字符串中获取一些价值,那么使用strtotime(“2012年10月8日”)会容易一百倍。然后,您可以根据需要格式化该日期,或使用第二个乘数来添加/减去该日期。