因爲我是新的PHP和谷歌搜索後:)我仍然無法找到我想要做的。之間的字符串,php
我已經能夠找到我想要提取的字符串中的開始和結束位置,但大多數示例使用字符串或字符或整數來獲取字符串,但我找不到兩個位置的字符串。
例如: $ string =「這是試圖提取的測試」; $ pos1 = 9; $ pos2 = 14;
然後我迷路了。我需要獲取字符串位置9和14之間的文本。 謝謝。
因爲我是新的PHP和谷歌搜索後:)我仍然無法找到我想要做的。之間的字符串,php
我已經能夠找到我想要提取的字符串中的開始和結束位置,但大多數示例使用字符串或字符或整數來獲取字符串,但我找不到兩個位置的字符串。
例如: $ string =「這是試圖提取的測試」; $ pos1 = 9; $ pos2 = 14;
然後我迷路了。我需要獲取字符串位置9和14之間的文本。 謝謝。
$startIndex = min($pos1, $pos2);
$length = abs($pos1 - $pos2);
$between = substr($string, $startIndex, $length);
<?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'
?>
您可以使用substr()來提取部分字符串。這可以通過設置你想要提取的起點和長度來工作。
所以你的情況,這將是:
$string = substr($string,9,5); /* 5 comes from 14-9 */
很酷。爲我工作。謝謝一堆! – Jackie