我如何解析网址? [英] How do I parse a url?

查看:67
本文介绍了我如何解析网址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,



什么是解析字符串并找出特定单词的最佳方法。



例如,



www.abc.com/public/test/this_is_test.php



现在在上面的网址中,我需要查找test和this_is_test并将其改为一个单词 - test_this_is_test并使用它重定向到另一个网站。



但是网址可能会有所不同,但有类似的模式。所以我不能只搜索this_is_test这个词..



希望它有意义..



谢谢

Hello,

What's a best way to parse a string and find out the particular word.

For instance,

www.abc.com/public/test/this_is_test.php

Now in the above url, I need to find out test and this_is_test and make it into one word - test_this_is_test and use it to redirect to another site.

But the url can vary but with a similar pattern. So I can not just search for the word this_is_test..

Hope it makes sense..

Thanks

推荐答案

Hai Andy,

您可以使用字符串数组并使用'/'分隔符将此网址分解为:

Hai Andy,
You cna use the string array and the break this URL with the '/' separator as below:
string url = "www.abc.com/public/test/this_is_test.php";
string[] splitString = url.Split('/');
var lastValue = string.Empty;
string lastPart = string.Empty;
for(int i = 0; i <splitstring.count;>{
 var secondValue = splitString[2].ToString();
 lastValue = splitString[3].ToString();
}



现在你将在单独的变量中得到test和this_is_test.php。

再次你需要拆分lastValue使用'。'并获得第一部分如下:


Now you will get the test and this_is_test.php in the separate variables.
Again you need to split the lastValue with the '.' and get the first part as below:

string[] lastSplit = lastValue.Split('.');
lastPart = lastSplit[0].ToString();



所以你的最终值将是secondValue和lastpart的连续符号如下:


So your final value will be the concatantion of the secondValue and lastpart as below:

string finalValue = secondValue + "_" + lastPart;



希望它对您有所帮助。


Hope it will be helpful to you.


这是否接近 mod_rewrite [ ^ ]领域如果正常工作在非Windows服务器上?这可能是值得研究的,因为它可以节省您的时间来编写其余的重定向。



如果这不是你想要的做(或你能做什么)然后一旦你做了上面的'转换',你可以做到以下几点:如果你设置一个至少有两列的数据库表(匹配和重定向网址的网址),那么你可以执行在该数据库表上选择一个查询以搜索test_this_is_test,并让它返回您希望它重定向到同一行第二列的URL。



一旦你有了想要将用户重定向到你的网址,就可以使用一个简单的位置调用来指向从数据库中提取的网址。



由于我们不知道您打算在系统中使用多少网址或可解析网址,因此很难说您可以轻松地执行此操作。您必须为每个可能传递给脚本的URL以及它应该重定向的位置都有一个数据库行。



您可以使用mod_rewrite来转发到外部网站。你只需要编写mod_rewrite规则,它应该如何匹配网址。但是,如果这对你不起作用,那么在执行前面提到的技巧以获得test_this_is_test之后,上面的建议也许对你有用。祝你好运!
Isn't this approaching close to mod_rewrite[^] territory if working on non-Windows server? That could be worth looking into if so as it can save you time with having to code out the rest of your redirect.

If that isn't what you want to do (or what you are able to do) then once you have done the above 'conversion' you could do the following: If you set up a database table with at least two columns (urls to match and redirect urls) then you can perform a select query on that database table to search for "test_this_is_test" and have it return the url that you want it to redirect to which would be in the second column of the same row.

Once you have the url that you want to redirect the user to you can use a simple 'location' call pointing to the url that was pulled from the database.

Since we do not know how many url's or parsable url's you intend to be used in your system it is hard to say how easily you can do this. You would have to have a database row for each and every url that could possibly be passed to the script and where to where it should redirect.

You can use mod_rewrite to 'forward' to external sites. You would just have to write the mod_rewrite "rules" for how it should match urls. But if that doesn't work for you then perhaps my suggestion above will work okay for you after performing the previously mentioned trick to get the "test_this_is_test". Best of luck!


查看 Uri班级 [ ^ ]



Uri类提供统一资源标识符(URI)的对象表示,并可轻松访问这些部分URI。

有许多方法可以帮助从URL字符串中获取正确的信息。
Check out Uri class[^] in .NET

Uri class provides an object representation of a uniform resource identifier (URI) and easy access to the parts of the URI.
There is many methods that can help in getting the right information from an URL string.


这篇关于我如何解析网址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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