删除 php 字符串中的一组特定字符

deleting a specific set of characters in a php string

下面是我所有编码的一小部分,这就是我需要的东西。 $linkurl 只是一个普通的 url。 我想要这段代码,这样可能的 http://https:// 就不会出现在 link 中,然后再将其放入我的数据库中。当人们自己添加 http:// 时,这是一种错误预防,因此您无法在数据库中获得 url 和 http://http://

if (strpos($linkurl,'http://') !== false){
    $linkurl-http://=$linkurl
}

问题是,我不知道在 if 语句中输入什么。

您可以使用正则表达式将 http://https:// 替换为这段代码。

$linkurl = 'http://example.com'; //Works for https too
$replaced = preg_replace('@(http://|https://)@i', '',  $linkurl);
echo $replaced;

lolka_bolka 答案是正确的,但是,您也可以使用非正则表达式版本:

$linkurl = 'http://example.com';
$toreplace = ['http://', 'https://']; // can add more things like ftp:// or whatever you like
$replaced = str_replace($toreplace, '', $linkurl);

echo $replaced;

如果您使用旧版本的 PHP,您需要使用 $toreplace = array('http://', 'https://'); 而不是