[英]PHP str_starts_with and str_ends_with functions not working as expected
I have a php file that works well when I run it on browser but when I schedule it to run via cpanel cronjob, strangely it doesn't work.我有一个 php 文件,当我在浏览器上运行它时运行良好,但是当我安排它通过 cpanel cronjob 运行时,奇怪的是它不起作用。 I spoke at length with the hosting company(namecheap) but they insisted the cron environment was ok, and the problem might be from my code.
我与托管公司 (namecheap) 进行了详细交谈,但他们坚持认为 cron 环境没问题,问题可能出在我的代码上。 So I decided to debug my code thoroughly to find out which functions were preventing the file from running via cronjob.
所以我决定彻底调试我的代码,找出哪些函数阻止文件通过 cronjob 运行。 This is the code I used for the debugging:
这是我用于调试的代码:
$funcs = ['str_starts_with', 'str_ends_with', 'writegames', 'is_array'];
foreach($funcs as $fun) {
if(function_exists($fun)){
echo $fun." exists\n";
} else {
echo $fun." doesn't exist\n";
}
}
//NB: writegames is a custom function I wrote, so it exists
Here is what I found out: ALL the functions echo 'EXISTS' when I run this code on browser, but when I schedule it via cpanel cronjob, 'str_starts_with' and 'str_end_with' echo 'DOESNT EXIST', how is this even possible?这是我发现的:当我在浏览器上运行这段代码时,所有函数都回显“存在”,但是当我通过 cpanel cronjob 安排它时,“str_starts_with”和“str_end_with”回显“不存在”,这怎么可能呢? I have PHP 8.0 enabled on my cpanel, I was wondering if there's some other thing which I was supposed to set which I didn't set or should I move to PHP 8.1?
我在我的 cpanel 上启用了 PHP 8.0,我想知道是否还有其他一些我应该设置但我没有设置的东西,或者我应该移动到 PHP 8.1 吗? At this point, I'm very confused.
在这一点上,我很困惑。 Thanks.
谢谢。
you can write the functions yourself.你可以自己写函数。
<?php
//str_starts_with
if(!function_exists('str_starts_with')) {
echo 'str_starts_with doesn\'t exist<br/>';
function str_starts_with($haystack,$needle,$trim_haystack=false) {
//str_starts_with(string $haystack, string $needle): bool
if($trim_haystack) {
$haystack = trim($haystack);
}
$strlen_needle = mb_strlen($needle);
if(mb_substr($haystack,0,$strlen_needle)==$needle) {
return true;
}
return false;
}
}
//str_ends_with
if(!function_exists('str_ends_with')) {
echo 'str_ends_with doesn\'t exist<br/>';
//str_ends_with(string $haystack, string $needle): bool
function str_ends_with($haystack,$needle,$trim_haystack=false) {
//str_starts_with(string $haystack, string $needle): bool
if($trim_haystack) {
$haystack = trim($haystack);
}
$strlen_needle = mb_strlen($needle);
if(mb_substr($haystack,-$strlen_needle,$strlen_needle)==$needle) {
return true;
}
return false;
}
}
// Tests:
if(str_starts_with('hello world','hello')) {echo 'start with hello';}
echo '<br/>';
if(str_ends_with('hello world','world')) {echo 'end with world';};
Just like @LawrenceCherone and @rickdenhaan pointed out in the comments.就像@LawrenceCherone 和@rickdenhaan 在评论中指出的那样。 My webserver was running on PHP 8 while my cron was running on PHP 7 because the cpanel was configured to PHP 7.
我的网络服务器在 PHP 8 上运行,而我的 cron 在 PHP 7 上运行,因为 cpanel 配置为 PHP 7。
I wrote to the Hosting agency to configure my cpanel to PHP 8 which they did within seconds and everything started working fine again.我写信给托管机构将我的 cpanel 配置为 PHP 8,他们在几秒钟内就完成了,一切又开始正常工作了。
Alternatively, assuming I didn't choose to configure my cpanel to PHP 8, this is the code I would have used, for PHP 7 up to PHP 4:或者,假设我没有选择将我的 cpanel 配置为 PHP 8,对于 PHP 7 到 PHP 4,这是我会使用的代码:
function str_starts_with($val, $str) {
if(mb_substr($val, 0, mb_strlen($str)) == $str) {
return true;
} else {
return false;
}
}
function str_ends_with($val, $str) {
if(mb_substr($val, -mb_strlen($str), mb_strlen($str)) == $str) {
return true;
} else {
return false;
}
}
To test it:测试它:
$string = 'stackoverflow';
$start = 'stack';
$end = 'flow';
if(str_starts_with($string, $start)) {
echo 'yes'; //echoes yes
} else {
echo 'no';
}
if(str_ends_with($string, $end)) {
echo 'yes'; //echoes yes
} else {
echo 'no';
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.