首页 > 解决方案 > 如何在php中删除前几个字母并打印最后一个标题

问题描述

我有永久链接和页面标题。我需要删除唯一的永久链接并使用 PHP 打印标题。这是代码: $string = "http://example.com/blog/PageTitle"

从这里删除http://example.com/blog/并仅打印PageTitle

我尝试过这个:

$string = 'http://example.com/blog/PageTitle';
$remove_text = 'http://example.com/blog/'
$count = strlen($remove_text)
echo substr($string, $count);

注意:我的$stringpageTitle是可变的http://example.com/about/PageTitlehttp://example.com/contact/PageTitle2等等。

标签: phpwordpresssubstr

解决方案


试试这个

$string = strrev($string);
$str_arr = explode ("/", $string);
$pageTitle = $str_arr[0];
$pageTitle = strrev($pageTitle);
echo $pageTitle;

这应该有效。


推荐阅读