PHP:抓取页面标题或WordPress用户昵称

PHP:抓取页面标题或WordPress用户昵称,php,wordpress,Php,Wordpress,我将首先解释我希望代码做什么 代码应该通过使用api显示游戏中玩家的数据 由游戏本身提供()。在我的网站上,每个玩家都有自己的页面,页面的标题是玩家的名字。因此,我想抓取页面的标题,以便在其下显示播放器的数据 (代码的另一部分完全正常工作。如果我删除$title行并将$url更改为播放器的url,它就正常工作了) 我无法使用下面的代码。 当我将此代码插入页面时,页面将断开。但是,如果我删除$title=alert(document.title);”行,页面不会中断 我在网上和stackoverf

我将首先解释我希望代码做什么

代码应该通过使用api显示游戏中玩家的数据 由游戏本身提供()。在我的网站上,每个玩家都有自己的页面,页面的标题是玩家的名字。因此,我想抓取页面的标题,以便在其下显示播放器的数据

(代码的另一部分完全正常工作。如果我删除$title行并将$url更改为播放器的url,它就正常工作了)

我无法使用下面的代码。 当我将此代码插入页面时,页面将断开。但是,如果我删除
$title=alert(document.title);”行,页面不会中断

我在网上和stackoverflow上做了一些研究,我试图在
str_get_html(“”)中更改这一行但它没有帮助

<?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 />";
}
?>
每个用户都将其播放器名称存储为“昵称”。因此,可以通过以下方式获取每个用户的玩家名称:

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

您可以尝试使用此函数解析$url返回的HTML

<?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;
    }
?>

希望这能有所帮助。

我已经尝试过该代码,但它不起作用,,,我现在已将页面设置为:
example.com/user/playername
(其中只有playername更改)。我现在正在使用:
global$post$pagename=$post->post\u name
但是,$pagename显示为
用户
而不是
播放名称
。你知道我如何让它“获取”播放名称而不是它的slug吗?所以你需要获取当前wordpress页面的标题,而不是
上的标题。”http://services.runescape.com/m=hiscore/index_lite.ws?player=$title“
?如果是的话,你可以尝试使用Yes,我必须得到当前页面的标题。我已经尝试了
获取标题()
函数,但没有成功。这可能是因为页面被设置为“用户”,而
playername
只是页面的一个id。因此,我认为真正的url是
example.com/user.php?id=playername
,但显示为
example.com/user/playername
。那么,如何尝试将permalink作为
$str
获取,然后使用
substr($str,strrpos($str,“/”)+1)仅保留其中最后一个“/”之后的零件?有关最后一部分的更多详细信息,请检查它是否正常工作!我知道我不应该仅仅为了说“谢谢你”而发表评论,但我必须这么做,因为我让你花了很多时间在这上面。。。非常感谢你!
function getPath($url)
{
$path = parse_url($url,PHP_URL_PATH);
$lastSlash = strrpos($path,"/");
return substr($path,1,$lastSlash-1);
}
<?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;
    }
?>
<?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'

?>