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 关注
- 69 浏览
添加回答
举报