PHP: Grab page title or WordPress user nickname

I will first explain what I want the code to do.

The code should show the data of a player in a game through the use of an api
provided by the game itself (http://services.runescape.com/m=hiscore/index_lite.ws?player=NameOfPlayer). On my website, each player has its own page, and the title of the page is the name of the player. Consequently, I would like to grab the title of the page in order to show the player’s data under it.

Read More

(The other part of the code is fully working. If I remove the $title line and change the $url to the one of a player it works).

I am having trouble to use the code below.
When I insert this code into a page, the page breaks. But, if I remove the $title = alert(document.title);'); line, the page does not break.

I did some research online and on stackoverflow, and I tried to change that line in str_get_html('<title></title>'); but it didn’t help.

<?php
$title = str_get_html('<title></title>');
$url = "http://services.runescape.com/m=hiscore/index_lite.ws?player=$title";
$f = file_get_contents($url);
$items = explode(' ', $f);
foreach($items as $item){
    $player = explode(",", $item);
    echo "Total Level: $player[1]<br />";
    echo "Attack: $player[3]<br />";
    echo "Defence: $player[5]<br />";
    echo "Strength: $player[7]<br />";
    echo "Constitution: $player[9]<br />";
    echo "Ranged: $player[11]<br />";
    echo "Prayer: $player[13]<br />";
    echo "Magic: $player[15]<br />";
    echo "Cooking: $player[17]<br />";
    echo "Woodcutting: $player[19]<br />";
    echo "Fletching: $player[21]<br />";
    echo "Fishing: $player[23]<br />";
    echo "Firemaking: $player[25]<br />";
    echo "Crafting: $player[27]<br />";
    echo "Smithing: $player[29]<br />";
    echo "Mining: $player[31]<br />";
    echo "Herblore: $player[33]<br />";
    echo "Agility: $player[35]<br />";
    echo "Thieving: $player[37]<br />";
    echo "Slayer: $player[39]<br />";
    echo "Farming: $player[41]<br />";
    echo "Runecrafting: $player[43]<br />";
    echo "Hunter: $player[45]<br />";
    echo "Construction: $player[47]<br />";
    echo "Summoning: $player[49]<br />";
    echo "Dungeoneering: $player[51]<br />";
    echo "Divination: $player[53]<br />";
    echo "Invention: $player[55]<br />";
}
?>

I was also thinking that it was possible to grab the data in another way, but I have no idea on how to do it.
The website runs on WordPress, and each player page is connected to the WordPress account of a registered user.
It is possible to grab the data of a user by using the following reference:

get_userdata( $userid );

Each user has its player name stored as “nickname”. Consequently, it is possible to grab the player name of each user by using:

<?php $user_info = get_userdata(1);
  echo $user_info->nickname;
?>

However, the user-pages are generated by a plugin. I have tried to tweak the code inside it, but I couldn’t manage to do it successfully.

Do you know how I can grab the title of the page without breaking it, or grab the nickname of a user?

I have now set the pages to look like: example.com/user/playername (where only playername changes).
I am now using:
global $post;
$pagename = $post->post_name;

However, $pagename is shown as user instead of playername. Do you know how I can make it “get” the playername instead of its slug?

EDIT 2:

function getPath($url)
{
$path = parse_url($url,PHP_URL_PATH);
$lastSlash = strrpos($path,"/");
return substr($path,1,$lastSlash-1);
}

Related posts

1 comment

  1. You could try parsing the HTML returned by the $url using this function

    <?php
        function page_title($url) {
            $fp = file_get_contents($url);
            if (!$fp) 
                return null;
    
            $res = preg_match("/<title>(.*)</title>/siU", $fp, $title_matches);
            if (!$res) 
                return null; 
    
            // Clean up title: remove EOL's and excessive whitespace.
            $title = preg_replace('/s+/', ' ', $title_matches[1]);
            $title = trim($title);
            return $title;
        }
    ?>
    

    You can find more details in this answer.

    EDIT

    Since you are looking to find the player name from your own URL you can use get_permalink().

    Then you need to extract the player name from a URL like https://example.com/user/eibe/.

    To do that you can use the following code:

    <?php
    
    $permalink = get_permalink();  // $permalink is now 'https://example.com/user/eibe/'
    
    $permalink = trim($permalink, "/"); // $permalink is now 'https://example.com/user/eibe'
    
    $player_name = substr($permalink, strrpos($permalink, '/') + 1); // $player_name is now 'eibe'
    
    ?>
    

    Hope this helps.

Comments are closed.