close

<?php
function cut_string_using_last($character, $string, $side, $keep_character=true) {
$offset = ($keep_character ? 1 : 0);
$whole_length = strlen($string);
$right_length = (strlen(strrchr($string, $character)) - 1);
$left_length = ($whole_length - $right_length - 1);
switch($side) {
case 'left':
$piece = substr($string, 0, ($left_length + $offset));
break;
case 'right':
$start = (0 - ($right_length + $offset));
$piece = substr($string, $start);
break;
default:
$piece = false;
break;
}
return($piece);
}

$example = 'http://example.com/path/file.php';
$cwd_relative[] = cut_string_using_last('/', $example, 'left', true);
$cwd_relative[] = cut_string_using_last('/', $example, 'left', false);
$cwd_relative[] = cut_string_using_last('/', $example, 'right', true);
$cwd_relative[] = cut_string_using_last('/', $example, 'right', false);
foreach($cwd_relative as $string) {
echo "$string <br>".PHP_EOL;
}
?>


輸出:
http://example.com/path/
http://example.com/path
/file.php
file.php

arrow
arrow
    全站熱搜

    stockwfj3 發表在 痞客邦 留言(0) 人氣()