如何从输入字段中删除特定的URL链接

时间:2022-01-07 08:15:11

I have search form with specific action

我有特定行动的搜索表单

<form method="get" action="info.php?short=" id="urlcreateform">
<input id="urlbox" name="short" placeholder="Some text" type="text">

Visitors of my site will use this form to find some stats, but they will past or write to input field full url link such as (http:/example.com/123456). And that's problem. I now how to remove http from input field, but I cant find answer how to remove url address such as http:/example.com/ from input field.

我网站的访问者将使用此表单查找一些统计信息,但他们将过去或写入输入字段的完整网址链接,例如(http:/example.com/123456)。这就是问题所在。我现在如何从输入字段中删除http,但我无法找到答案如何从输入字段中删除http:/example.com/等URL地址。

Does anybody now how to do it? I can't find nothing.

现在有人怎么做?我找不到任何东西。

2 个解决方案

#1


1  

You can do this with .replace():

您可以使用.replace()执行此操作:

var urlNumbers = $("#urlbox").val().replace("example.com/", "");

If you want to be safe and also remove any possible http:// or www. then use the following regex:

如果您想要安全并且还删除任何可能的http://或www。然后使用以下正则表达式:

var urlNumbers = $("#urlbox").val().replace(/example.com\/|http:\/\/|www./gi, "");

#2


1  

If your URL always looks like 'http:/example.com/123456' then you can just substring it at the last "/".

如果您的网址总是看起来像'http:/example.com/123456'那么您可以在最后一个“/”处对其进行子串。

In Javascript, you can detect the last "/" and split it like this:

在Javascript中,您可以检测到最后一个“/”并将其拆分为:

var originalUrl = $("#urlbox").val();
var urlNumbers = originalUrl.substring(originalUrl.lastIndexOf("/"));

However, it is better to do this on the PHP side (and work more securely with POST instead of GET):

但是,最好在PHP端执行此操作(并使用POST而不是GET更安全地工作):

$originalUrl = $_POST["short"];
$urlNumbers = substr($originalUrl, strrpos($originalUrl, "/"));

Documentation:

#1


1  

You can do this with .replace():

您可以使用.replace()执行此操作:

var urlNumbers = $("#urlbox").val().replace("example.com/", "");

If you want to be safe and also remove any possible http:// or www. then use the following regex:

如果您想要安全并且还删除任何可能的http://或www。然后使用以下正则表达式:

var urlNumbers = $("#urlbox").val().replace(/example.com\/|http:\/\/|www./gi, "");

#2


1  

If your URL always looks like 'http:/example.com/123456' then you can just substring it at the last "/".

如果您的网址总是看起来像'http:/example.com/123456'那么您可以在最后一个“/”处对其进行子串。

In Javascript, you can detect the last "/" and split it like this:

在Javascript中,您可以检测到最后一个“/”并将其拆分为:

var originalUrl = $("#urlbox").val();
var urlNumbers = originalUrl.substring(originalUrl.lastIndexOf("/"));

However, it is better to do this on the PHP side (and work more securely with POST instead of GET):

但是,最好在PHP端执行此操作(并使用POST而不是GET更安全地工作):

$originalUrl = $_POST["short"];
$urlNumbers = substr($originalUrl, strrpos($originalUrl, "/"));

Documentation: