Line between, php

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

I managed to find the start and end position in the string that I want to extract, but most examples use strings or characters or integers to get a string between them, but I could not find a string consisting of two positions.

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

Then I got lost. I need to get text between positions 9 and 14 of a line. Thank you

+6
source share
3 answers
$startIndex = min($pos1, $pos2); $length = abs($pos1 - $pos2); $between = substr($string, $startIndex, $length); 
+8
source

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 it will be:

 $string = substr($string,9,5); /* 5 comes from 14-9 */ 
+3
source
 <?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' ?> 
+1
source

Source: https://habr.com/ru/post/892591/


All Articles