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

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

问题描述

示例用户输入

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

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

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

让我知道: 一系列不允许的词汇并使用 strpos str_replace 将其从传入的URL中动态移除:

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


Example user input

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

I want a php function to make the output like

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

Let me know :)

解决方案

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天全站免登陆