删除字符串开头和结尾的内容 (PHP)

发布于 2024-09-29 16:54:34 字数 353 浏览 0 评论 0原文

我试图从字符串中获取用户 ID,例如:

http://www.abcxyz.com/123456789 /

显示为 123456789 本质上是剥离第一个 / 之前的信息,并删除末尾的 /。我确实在网上浏览过,但似乎有很多解决方案,但没有任何答案可以回答开始和结束。

谢谢:)

更新 1

该链接可以采用两种形式:如上所述的 mod_rewrite 以及“http://www.abcxyz.com/profile?user_id=123456789”

I'm trying to get a users ID from a string such as:

http://www.abcxyz.com/123456789/

To appear as 123456789 essentially stripping the info up to the first / and also removing the end /. I did have a look around on the net but there seems to be so many solutions but nothing answering both start and end.

Thanks :)

Update 1

The link can take two forms: mod_rewrite as above and also "http://www.abcxyz.com/profile?user_id=123456789"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

江心雾 2024-10-06 16:54:34

我会使用 parse_url()从 URL 中干净地提取路径组件:

$path = parse_URL("http://www.example.com/123456789/", PHP_URL_PATH);

然后使用 explode() 将路径拆分为其元素:

$path = trim($path, "/"); // Remove starting and trailing slashes
$path_exploded = explode("/", $path);

然后输出路径的第一个组件:

echo $path_exploded[0]; // Will output 123456789

此方法适用于像

  • 这样的边缘情况>http://www.example.com/123456789?test
  • http://www.example.com//123456789
  • www.example.com/123456789/abcdef< /code>

甚至

  • /123456789/abcdef

I would use parse_url() to cleanly extract the path component from the URL:

$path = parse_URL("http://www.example.com/123456789/", PHP_URL_PATH);

and then split the path into its elements using explode():

$path = trim($path, "/"); // Remove starting and trailing slashes
$path_exploded = explode("/", $path);

and then output the first component of the path:

echo $path_exploded[0]; // Will output 123456789

this method will work in edge cases like

  • http://www.example.com/123456789?test
  • http://www.example.com//123456789
  • www.example.com/123456789/abcdef

and even

  • /123456789/abcdef
眼趣 2024-10-06 16:54:34
$string = 'http://www.abcxyz.com/123456789/';
$parts = array_filter(explode('/', $string));
$id = array_pop($parts);
$string = 'http://www.abcxyz.com/123456789/';
$parts = array_filter(explode('/', $string));
$id = array_pop($parts);
霞映澄塘 2024-10-06 16:54:34

如果 ID 始终是 URL 的最后一个成员

$url="http://www.abcxyz.com/123456789/";
$id=preg_replace(",.*/([0-9]+)/$,","\\1",$url);
echo $id;

If the ID always is the last member of the URL

$url="http://www.abcxyz.com/123456789/";
$id=preg_replace(",.*/([0-9]+)/$,","\\1",$url);
echo $id;
要走就滚别墨迹 2024-10-06 16:54:34

如果 URL 中没有其他数字,您还可以

echo filter_var('http://www.abcxyz.com/123456789/', FILTER_SANITIZE_NUMBER_INT);

删除所有非数字的内容。

这可能比使用 parse_url+parse_str 组合要快一些。

If there is no other numbers in the URL, you can also do

echo filter_var('http://www.abcxyz.com/123456789/', FILTER_SANITIZE_NUMBER_INT);

to strip out everything that is not a digit.

That might be somewhat quicker than using the parse_url+parse_str combination.

豆芽 2024-10-06 16:54:34

如果您的域不包含任何数字,您可以使用以下方法处理这两种情况(有或没有 user_id):

<?php

$string1 = 'http://www.abcxyz.com/123456789/';
$string2 = 'http://www.abcxyz.com/profile?user_id=123456789';

preg_match('/[0-9]+/',$string1,$matches);
print_r($matches[0]);


preg_match('/[0-9]+/',$string2,$matches);
print_r($matches[0]);


?>

If your domain does not contain any numbers, you can handle both situations (with or without user_id) using:

<?php

$string1 = 'http://www.abcxyz.com/123456789/';
$string2 = 'http://www.abcxyz.com/profile?user_id=123456789';

preg_match('/[0-9]+/',$string1,$matches);
print_r($matches[0]);


preg_match('/[0-9]+/',$string2,$matches);
print_r($matches[0]);


?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文