How the right from the left string

The string for the:".. There are a lot of.. 001_002_003 ", want from the right side of the fourth (_003), get all the string to the left".. There are a lot of. . 001_002"

How to realize, the length of the string is uncertain, so the only right from the start loading

Started by Ailsa at October 29, 2016 - 2:38 AM

Substr can use negative numbers, details refer to:
http://cn2.php.net/manual/zh/function.substr.php

Posted by Kerwin at November 09, 2016 - 3:14 AM

You can use the "_" as the separator, the array operation.

Posted by Gillian at November 16, 2016 - 3:23 AM

[code=php][<?php
$str='001_002_003';
$find='_003';
echo substr($str,0,strpos($str,$find))
?>/code]

Posted by Borg at November 29, 2016 - 3:49 AM

<?php
$str='001_002_003';
$find='_003';
echo substr($str,0,strpos($str,$find))
?>

Posted by Borg at December 03, 2016 - 4:44 AM

$s = '001_002_003';
echo substr($s, 0, -4);//001_002
echo substr($s, 0, strrpos($s, '_'));//001_002
echo join('_', array_slice(explode('_', $s), 0, -1));//001_002
echo strrev(substr(strrev($s), 4));//001_002

Posted by Morgan at December 17, 2016 - 5:17 AM

$str = '001_002_003';
$data = explode('_',$str);
krsort($data);
foreach($data as $row)
echo $row.'<br>';

Posted by Jacqueline at December 27, 2016 - 5:55 AM

$str = '001_002_003_004_005_006_007';
$data = explode('_',$str);
krsort($data);
$total = count($data);
$need = $total - 3;
foreach($data as $k=>$row){
	if($k<$need){
		echo $row.'<br>';
	}
}

Posted by Jacqueline at January 01, 2017 - 8:51 AM

Not see the subject, the 6 floor is not, the 7 floor should be LZ requirements.?

Posted by Jacqueline at January 04, 2017 - 6:41 AM

Reference resources:
<?php
$rest = substr("abcdef", 0, -1);  // Return "abcde"
$rest = substr("abcdef", 2, -1);  // Return "cde"
$rest = substr("abcdef", 4, -4);  // Return ""
$rest = substr("abcdef", -3, -1); // Return "de"
?>

Posted by Heidi at January 07, 2017 - 7:35 AM

Substr can be used with negative is positive solution

Posted by Tamara at January 14, 2017 - 8:16 AM