开发者

string between, php

as I am new to php, and after googling :) I still could not find what I wanted to do.

I have been able to find start and end position in string that i want to extract but most of the example use strings or characters or integers to get string between but I could not find str开发者_开发技巧ing bewteen two positions.

For example: $string = "This is a test trying to extract"; $pos1 = 9; $pos2 = 14;

Then I get lost. I need to get the text between position 9 and 14 of of the string. Thanks.


$startIndex = min($pos1, $pos2);
$length = abs($pos1 - $pos2);

$between = substr($string, $startIndex, $length);


You can use substr() to extract part of a string. This works by setting the starting point and the length of what you want to extract.

So in your case this would be:

$string = substr($string,9,5); /* 5 comes from 14-9 */


<?php
$string = "This is a test trying to extract";
$pos1 = 9;
$pos2 = 14;

$start = min($pos1, $pos2);
$length = abs($pos1 - $pos2);

echo substr($string, $start - 1, $length); // output 'a test'
?>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜