开发者

How do i remove a chunk of text from a variable? (PHP)

I need to be able to remove a URL from a variable, I'm wondering how i do this. Example - Say my script returns h开发者_开发技巧ttp://www.example.com/file.php?id=1234 i need to be able to remove the http://www.example.com/file.php?id= bit, just leaving the id number. If anyone can help, it would be great :)


Something like this?

$var = 'http://www.example.com/file.php?id=1234';
$query = parse_url($var, PHP_URL_QUERY);
$query_components = parse_str($query);

$id = $query_components['id'];


You can use regular expressions:

preg_match("/id=(\\d+)/", $url, $matches);
$id = $matches[1];


Just use $id = $_GET['id'];.

See the docs.

And don't forget to validate and sanitize.


The "id" in this case is being sent to your script as a GET variable, therefore you would access it as follows:

    $id = $_GET['id'];

If you mean to say that this URL is not yours to control, then you would do this instead:

    print_r(parse_url($url)); // Then analyze the output.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜