0

i want get last id from url, i have url from text file like this

http://domain.com/..../pid=87dhdhjkh&tid=87ehjdhnjh

how to get output 87ehjdhnjh

i have this code

$file = fopen("file.txt", "r");
while(!feof($file)){
$line = fgets($file);
# do same stuff with the $line


$str = ($line);


if(preg_replace("/.+?\.*/", "$str", $m)){
echo  $m[1] .'<br>' ;
}

but not work

dexter
  • 123
  • 9

3 Answers3

2

You need to just parse your url.

Let's say you have variable callde $url storing your url as a string, if you just use parse_url() function here is what you get:

$url = "http://domain.com/.../pid=87dhdhjkh&tid=87ehjdhnjh";

$result = parse_url($url);

You will get this as an output:

Array (
    [scheme] => http
    [host] => domain.com
    [path] => /.../pid=87dhdhjkh&tid=87ehjdhnjh 
)

After that you can get path from that array and parse it using parse_str() function:

parse_str($result['path'], $output);

print_r($output);

The output will be following:

Array
(
    [/___/pid] => 87dhdhjkh
    [tid] => 87ehjdhnjh
)

Now that array is stored in $output and you can retrieve value of tid from it, which is what you needed:

print $output['tid'];

So to summarize here is everything you need:

$url = "http://domain.com/.../pid=87dhdhjkh&tid=87ehjdhnjh";

$result = parse_url($url);
parse_str($result['path'], $output);
print $output['tid'];

UPDATE:

This question and answer might be considered as duplicate of this: https://stackoverflow.com/a/11480852/2267244

Community
  • 1
  • 1
otarza
  • 211
  • 2
  • 8
0

Try this:

$input_value = 'http://domain.com/..../pid=87dhdhjkh&tid=87ehjdhnjh';
$last = explode('=', strstr($input_value, '&tid'));
echo $last[1];
Ivnhal
  • 1,099
  • 1
  • 12
  • 20
0

You can use standard preg_match function

$line = 'http://domain.com/.../pid=87dhdhjkh&tid=87ehjdhnjh';
$pattern = '/\&tid\=([^\&]+)/';

preg_match($pattern, $line, $matches, PREG_OFFSET_CAPTURE);

if(count($matches) > 0) {
    echo $matches[1][0];
}
Mateusz Palichleb
  • 765
  • 12
  • 20