php函數stripos高級用法 檢查字符串是否以某個字符串開頭或者結尾

1. 使用 stripos 函數來檢查字符串是否以某個字符串開頭:

<?php

$string = "Hello world";
$needle = "Hello";
if (stripos($string, $needle) === 0) {
    echo $string . " starts with " . $needle;
} else {
    echo $string . " does not start with " . $needle;
}

// Output:
// Hello world starts with Hello

?>

2. 使用 stripos 函數來檢查字符串是否以某個字符串結尾:

<?php

$string = "Hello world";
$needle = "world";
if (stripos($string, $needle) === strlen($string) - strlen($needle)) {
    echo $string . " ends with " . $needle;
} else {
    echo $string . " does not end with " . $needle;
}

// Output:
// Hello world ends with world

?>

這些示例只是 stripos 函數的一部分高級用法,實際上,該函數還具有很多其他功能,您可以根據需要進一步學習和了解。希望本篇文章對您有所幫助。

發布者:彬彬筆記,轉載請註明出處:https://www.binbinbiji.com/zh-hant/php/3061.html

(0)
彬彬筆記彬彬筆記
上一篇 2023年2月14日
下一篇 2023年2月19日

相關推薦

發表回復

登錄後才能評論
蜀ICP備14017386號-13