如何检查字符串是否以指定的字符串开头? [英] How to check if a string starts with a specified string?

查看:99
本文介绍了如何检查字符串是否以指定的字符串开头?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试检查字符串是否以http开头.我该如何检查?

I'm trying to check if a string starts with http. How can I do this check?

$string1 = 'google.com';
$string2 = 'http://www.google.com';

推荐答案

使用 substr 函数返回一个字符串的一部分.

Use the substr function to return a part of a string.

substr( $string_n, 0, 4 ) === "http"

如果您要确保它不是另一个协议.我会改用http://,因为https也会匹配,以及其他诸如http-protocol.com之类的东西.

If you're trying to make sure it's not another protocol. I'd use http:// instead, since https would also match, and other things such as http-protocol.com.

substr( $string_n, 0, 7 ) === "http://"

一般来说:

substr($string, 0, strlen($query)) === $query

这篇关于如何检查字符串是否以指定的字符串开头?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆