1 回答

TA貢獻1779條經驗 獲得超6個贊
您可以通過使用來做到這一點explode,然后通過指定從右側開始的位置來提取所需的值:
<?php
function extractText($str)
{
// Break the string into an array
$exp = explode('-', $str);
// `count` will return number of elements in an array.
// Since array index starts from zero, to retrieve 2nd last element we need to use `-2`
// Remove spaces at the start and end of the text by using `trim` function
return trim($exp[count($exp) - 2]);
}
echo extractText('This is some-text for - Extracted Text - random'); // Extracted Text
echo PHP_EOL;
echo extractText('This is-some-text for - Extracted Text - another random'); // Extracted Text
echo PHP_EOL;
echo extractText('This-is a third-text for - Extracted Text - more random'); // Extracted Text
?>
- 1 回答
- 0 關注
- 121 瀏覽
添加回答
舉報