Parse transfermarket link - php

how to get ID(the numbers at the end) from links transfermarket site in PHP?
for example http://www.transfermarkt.co.uk/claudio-bravo/profil/spieler/40423
$url = 'http://www.transfermarkt.co.uk/andriy-pyatov/profil/spieler/40423';
$r = parse_url($url);
$endofurl = substr($r['path'], strrpos($r['path'], '/'));
$endofurl returns /40423
how to get rid of / ?

Solution A
Assuming
$url = "http://www.transfermarkt.co.uk/andriy-pyatov/profil/spieler/40423";
you could use explode to split the url and then end to get the last element (40423 in this example):
$id = explode("/", $url);
$id = end($id);
Solution B
In case you really want to use $endofurl:
$id = substr($endofurl, 1);

Related

php explode and foreach returens only one loop

I am not very professional but
I have this PHP code :
$m = mysqli_query($dblink,"select * from bot where type='movie' and ID ='$wait'");
while($a = mysqli_fetch_array($m, MYSQLI_ASSOC)){
$ex = explode(",",$a["links"]);
preg_match_all('/\b(https?|ftp|file):\/\/[-A-Z0-9+&##\/%?=~_|$!:,.;]*[A-Z0-9+&##\/%=~_|$]/i', $ex[1], $matches);
$urls = $matches[0];
foreach($urls as $url){
$s=size($url);
}
preg_match_all('/\b(https?|ftp|file):\/\/[-A-Z0-9+&##\/%?=~_|$!:,.;]*[A-Z0-9+&##\/%=~_|$]/i', $ex[1], $matches2);
$urls2 = $matches2[0];
}
and I also have http://site1.com,http://site2.com,http://site3.com in my "links" column of database.
so here is my problem : the code only shows one of the sites.
I was wondering if the problem is with foreach or explode or non of them?
thank all of you in advance!
EDIT :
so I changed my code to this :
$ex = explode(",",$a["links"]);
$url=$a["links"];
foreach($ex as $url){
$s=size($url);
...
}
and it seems to be a problem with $url because when I use a custom url for example http://test.com instead of $url it works and shows me 3 links which is the number of links in my database with the url of http://test.com.
What am I doing wrong with $url ?
$ex = explode(",",$a["links"]);
Will return an array of values (links) as follows:
$ex[0] = 'http://site1.com'
$ex[1] = 'http://site2.com'
$ex[2] = 'http://site3.com'
And you are passing only the second position of the array (index = 1 => $ex[1]) to the next line
preg_match_all('/\b(https?|ftp|file):\/\/[-A-Z0-9+&##\/%?=~_|$!:,.;]*[A-Z0-9+&##\/%=~_|$]/i', $ex[1], $matches);
so, you can do this:
foreach($ex as $url) {
preg_match_all('/\b(https?|ftp|file):\/\/[-A-Z0-9+&##\/%?=~_|$!:,.;]*[A-Z0-9+&##\/%=~_|$]/i', $url, $matches);
}
to get the matches for each url in the links database column

Get URL and remove id from it

I have a url like this
url:- url.php?gender=male&&grand=brand1&&id=$id
eg. $id may be 1, 100 ,23, 1000 any number
I am getting the url using
<?php echo $_SERVER['REQUEST_URI']; ?>
Is it possible to change the id and make the url like
url:- url.php?gender=gender&&brand=brand1&&id=$newId
where $newId can be any number except the one that is present in the url
function remove_querystring_var($url, $key) {
$url = preg_replace('/(.*)(?|&)' . $key . '=[^&]+?(&)(.*)/i', '$1$2$4', $url . '&');
$url = substr($url, 0, -2);
return $url;
}
this will do the job, pass your url and key you want to remove in this function
ex remove_querystring_var("url.php?gender=male&&grand=brand1&&id=$id","id"), it will remove id from your url
Get the id position and the remove the id using sub string.
$url = $_SERVER['REQUEST_URI'];
#Get id position and remove && by subtracting 2 from length
$pos = strrpos($url,'id') - 2;
$url = substr($url, 0, $Pos);
echo $url;
You could use $_SERVER['QUERY_STRING'] instead.
Moreover, if your 'id' value is not always at the end, getting a substr up to it wouldn't be very robust. Instead turn it into an associative array and fiddle with it.
For example, starting with /url.php?gender=male&grand=brand1&id=999&something=else
parse_str($_SERVER['QUERY_STRING'], $parsed);
unset($parsed['id']);
// there may be a better way to do this bit.
$new_args = [];
foreach($parsed as $key=>$val){
$new_args[] = "$key=$val";
}
$new_arg_str = join($new_args, '&');
$self = $_SERVER['PHP_SELF'];
$new_endpoint = "$self?$new_arg_str";
echo $new_endpoint;
result: /url.php?gender=male&grand=brand1&something=else
Bit more legible than regex as well.
Bear in mind if you're going to redirect (using header("Location: whatever") or otherwise), you'll need to be wary of redirect loops. An if statement could avoid that.
References:
http://php.net/manual/en/reserved.variables.server.php
http://php.net/manual/en/function.parse-str.php
http://php.net/manual/en/function.unset.php
http://php.net/manual/en/control-structures.foreach.php
http://php.net/manual/en/function.join.php

How to convert my URLs in two variable parts using PHP?

I want to convert my URLs into two parts e.g I have 4 urls like
c/category1
c/category2
p/page1
p/page2
Now what I extractly want is to convert URLs into two parts e.g.
$veriable_name1 = c
$veriable_name2 = category 1
or
$veriable_name1 = c
$veriable_name2 = category 2
or
$veriable_name1 = p
$veriable_name2 = page 1
or
$veriable_name1 = p
$veriable_name2 = page 2
In above examples C determines category and P determines Page. As i followed a tutorial to convert PHP urls into SEO friendly urls with PHP, but they only gave an example to convert urls by using PHP/MySQL from single table.
The code which i followed:
<?php
include('db.php');
if ($_GET['url']) {
$url = mysql_real_escape_string($_GET['url']);
$url = $url.'.html'; //Friendly URL
$sql = mysql_query("select title,body from blog where url='$url'");
$count = mysql_num_rows($sql);
$row = mysql_fetch_array($sql);
$title = $row['title'];
$body = $row['body'];
} else {
echo '404 Page.';
} ?>
HTML Part
<body>
<?php
if ($count) {
echo "<h1>$title</h1><div class='body'>$body</div>";
} else {
echo "<h1>404 Page.</h1>";
} ?>
</body>
but i have multiple links on my page and want to extract from multiple tables if other best suggestions
You need something like this:
$url = "c/category1";
$str_pos = strpos($url, "/");
$url_1 = substr($url, 0, $str_pos);
$url_2 = substr($url, $str_pos+1);
echo $url_1."<br/>";
echo $url_2;
Result:
c
category1
All your example "strings" are quite equal, so the easiest thing to do will be:
$url = 'c/category1';
$a = explode('/',$url);
$veriable_name1 = $a[0];
$veriable_name2 = $a[1];
or, to keep it a string function, you could run
$variable_name1 = strstr($url,'/',true);
$variable_name2 = str_replace('/','',strstr($url,'/'));
You can use explode for that:
$str = "c/category1";
list($variable_name1, $variable_name2) = explode('/', $str, 2);
var_dump($variable_name1);
var_dump($variable_name2);
//Output:
//string(1) "c" string(9) "category1"
U can use explode fn to do this. I have tried with strstr function.
Reference to strstr fn http://php.net/manual/en/function.strstr.php
$str = 'c/category';
$var2 = trim(strstr($str, '/'),'/');
echo $var2; // prints category
$var1 = strstr($str, '/', true); // As of PHP 5.3.0
echo $var1; // prints c
Note: it is applicable for [single slash oly] like c/category not for c/category/smg
You can also use a regular expression:
$url = "c/category1";
preg_match('/^(.+)\/(.+)$/', $url, $matches);
// check that the URL is well formatted:
if (count($matches) != 3) {die('problem');}
$veriable_name1 = $matches[1];
$veriable_name2 = $matches[2];
echo('"'.$veriable_name1.'" | "'.$veriable_name2.'"');
// it will show this: "c" | "category1"
Test the regex online [regex101.com].

php getting part of a URL into a string

Hi guys i m using this code to get the id on my url
$string = $url;
$matches = array();
preg_match_all('/.*?\/(\d+)\/?/s', $string, $matches);
$id = $matches[1][0];
this code works for urls like
http://mysite.com/page/1
http://mysite.com/page/somepage/2
http://mysite.com/page/3/?pag=1
i will have id = 1 / id = 2 / id = 3
but for a url like this
http://mysite.com/page/122-page-name/1
this returns id = 122
THe id i m try to get always will be the last part of the url or will have /?p= after
so the urls type i can have
http://mysite.com/page/1
http://mysite.com/page/some-page/2
http://mysite.com/page/12-some-name/3
http://mysite.com/page/some-page/4/?p=1
http://mysite.com/page/13-some-page/5/?p=2
id = 1 / id = 2 / id = 3 / id = 4 / id = 5
If your id will always be located at the end of your url, you could explode the contents of your url and take the last element of the resulting array. If it may include variables (like ?pag=1) you can add a validation after the explode to check for the variable.
$urlArray = explode('/', $url);
$page = end($urlArray);
if(strpos($page, 'pag')!==false){
//get the contents of the variable from the $page variable
//exploding the variable through the ? variable and getting
//the numeric characters at the end
}
I would favor URL parsing over trying to use a regex, especially if you have a wide variety of (valid) URLs to deal with.
end(array_filter(explode('/', parse_url($url, PHP_URL_PATH))));
The array_filter deals with the trailng slash.
Since it's always at the end or has ?p=x after it you can do the following:
$params = explode('/', $_SERVER['REQUEST_URI']);
$c = count($params);
if (is_int($params[$c - 1])
$id = $params[$c - 1];
else
$id = $params[$c - 2];
Not a direct answer, more of a "how to work this out for yourself" answer :]
Place this code at the top of your page, before anything else (but after <?php)
foreach($_SERVER as $k => $v) {
echo $k.' = '.$v.'<br />';
}
exit;
Now load up each of the different URIs in a different tab and look at the results. You should be able to work out what you need to do.

clearing the link id

if we have
$id = 39827-key1-key2-key3
and we want to show only the number or anything before (-)
then by using
$realid = array_shift(explode("-", $id));
we will get echo $realid; // 39827
Now my problem is as following !
if we have $id = key1/key2
and i want any way that remove the whole part key1/ and gives me only key2
how can i do it?
Ok, from your comment above, I'm assuming you want to do something like:
$id = "key1/key2";
$result = ???;
// Now $result=="$key2"
Why not just:
$parts = explode("/", $id);
$result = $parts[1];
Using the strstr() function, which was created exactly for things like this:
$id = 'key1/key2';
$realid = strstr($id, '/', true);
Do note that you have to be running PHP 5.3 or newer for this to work.
confusing question. my interpretation for $rawId containing the '/' char:
$rawId = 'key1/key2';
$realId = substr($rawId, 1 + strpos($rawId, '/')); // key2
Yet another way:
$result = implode('', array_slice(explode('/', $id), 1, 1));

Categories