现在我在我的字符串substr($row->review[$i] , 0, 120)
上使用这个,但我想更进一步,在我限制它之后,找到最后一个句点,并取出之后的所有内容。有什么想法吗?
$a = 'string you got with substr. iwehfiuhewiufh ewiufh . iuewfh iuewhf
iewh fewhfiu h. iuwerfh iweh f.ei wufh ewifh iuhwef';
$p = strrpos($a, '.');
if ($p !== false) // Sanity check, maybe there isn't a period after all.
$a = substr($a, 0, $p + 1 /* +1 to include the period itself */);
echo $a;
正如Alex所指出的,strrpos()可以找到子字符串最后一次出现的位置:
$offset = strrpos($row->review[$i],'.');
然后使用这个偏移量来分割主变量的最后一部分:
echo substr($row->review[$i],$offset);
请参阅有关strrpos()
的文档。
这是一个相当简单的解决方案,无论扩展名有多长,字符串中有多少点或其他字符,它都能工作。
$filename = "abc.def.jpg";
$newFileName = substr($filename, 0 , (strrpos($filename, ".")));
//$newFileName will now be abc.def
基本上这只是寻找的最后一次出现。然后使用子字符串来检索到该点为止的所有字符。
它类似于你在谷歌上搜索的一个例子,但比正则表达式和其他例子更简单、更快、更容易。好吧,无论如何。希望它能帮助到别人。