如何使用word_limiter获取第4和第7个单词

时间:2018-03-06 18:14:47

标签: php codeigniter

HI; 我想用word_limieter作为我的字符串 例如

$string="this is a story of a man who was live in LA";
echo word_limiter($string, 4);

它在屏幕上打印:这是一个故事

但是我希望得到第4和第7个单词(男人的故事)

怎么办呢?

2 个答案:

答案 0 :(得分:2)

您无法使用word_limiter()执行此操作,但可以使用array_slice()执行此操作。注意起始字是它在数组中的位置:

$string="this is a story of a man who was live in LA";
$wordArr = explode(' ', $string);
$new = array_slice($wordArr, 3, 4); // starting word and length
$sentence = implode(' ', $new);
echo $sentence;

您可以在传递字符串和数组位置的地方编写一个函数:

function new_word_limiter($string, $start, $end) {
    $wordArr = explode(' ', $string);
    $new = array_slice($wordArr, $start, $end);
    $sentence = implode(' ', $new);
    return $sentence;
}

然后这样称呼:

new_word_limiter($string, 3, 4); // either echo or assign to variable

答案 1 :(得分:0)

试试这个:) def get_pcapng_section_header(input_file): # The Section Header Block is special because it defines the endian. # Because of this get_pcapng_block (which requires endian) won't be used # until the endian is determined. section_header_block_file_position = input_file.tell() # The endian doesn't matter for the Section Header Block's Block Type. block_type = bytes_to_int(input_file.read(4), "big") assert(block_types[block_type] == "Section Header Block"), \ "Block Type %d is not Section Header Block at 0x%08x" % (block_type, section_header_block_file_position) # Skip the length for now. input_file.read(4)

substr(string,start,length)

为了您的目的,您可以使用 // SUBSTR($串,5,15)