如何从 php 中的用户输入中删除 http、https 和斜杠 [英] How do I remove http, https and slash from user input in php

查看:39
本文介绍了如何从 php 中的用户输入中删除 http、https 和斜杠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

示例用户输入

http://domain.com/
http://domain.com/topic/
http://domain.com/topic/cars/
http://www.domain.com/topic/questions/

我想要一个 php 函数来使输出像

I want a php function to make the output like

domain.com
domain.com/topic/
domain.com/topic/cars/
www.domain.com/topic/questions/

让我知道:)

推荐答案

您应该使用一系列不允许"的术语并使用 strposstr_replace 从传入的 URL 中动态删除它们:

You should use an array of "disallowed" terms and use strpos and str_replace to dynamically remove them from the passed-in URL:

function remove_http($url) {
   $disallowed = array('http://', 'https://');
   foreach($disallowed as $d) {
      if(strpos($url, $d) === 0) {
         return str_replace($d, '', $url);
      }
   }
   return $url;
}

这篇关于如何从 php 中的用户输入中删除 http、https 和斜杠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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