How can I change the <title> tag dynamically in php based on the URL values

杀马特。学长 韩版系。学妹 提交于 2019-12-10 10:52:51

问题


I want the title page to be changed so that a crawler can see it.

The URL is of the format: public.sample.com/account/Disney

I load a standard, global header include file using require()

That's where the current default tags are defined.

IF the URL is public.sample.com/account/Disney, I would like the tag to read, instead:

This is an account profile for Disney

I believe something would need to be written in this global header file, but not sure what.

Thanks.


回答1:


Super simple example to get you started.

<title><?php
if ( preg_match('/([^\/]+)\/([^\/]+)$/', $_SERVER['PHP_SELF'], $matches) ) {
    list($dummy, $profileType, $profileName) = $matches;
    $safeProfileType = htmlentities($profileType);
    $safeProfileName = htmlentities ($profileName);
    echo "This is an $safeProfileType profile for $safeProfileName";
} else {
    echo "Unknown profile!";
}
?></title>

Edited per Allain Lalonde comment. (Hope this is what he meant)




回答2:


im not sure exactly what you are looking for here, but to have a dynamic title you can do:

<title><?php echo $theDynamicTitle; ?></title>


来源:https://stackoverflow.com/questions/441565/how-can-i-change-the-title-tag-dynamically-in-php-based-on-the-url-values

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!