dongwen4487 2014-12-01 00:18
浏览 36
已采纳

如何使用PHP在两个可变部分中转换我的URL?

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

  • 写回答

5条回答 默认 最新

  • drzbc6003 2014-12-01 00:28
    关注

    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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度