从URL中提取ID

时间:2011-09-03 10:51:57

标签: php mysql .htaccess

这是在htaccess中写入链接的链接:

 RewriteRule ^carti-Dragoste/([a-zA-Z0-9_$\-]+)\.html$ book.php        

这是从链接中读取ID的代码:

 $id_tot=explode("_",$_SERVER['REQUEST_URI']);    
$id=$id_tot['1'];    
$id1=substr($id,0,strlen($id)-5);

这是查询:

 $query="SELECT * FROM carte WHERE id=" . $id1;    
 $res=mysql_query($query) or die(mysql_error());    
 while ($row=mysql_fetch_array($res))    
 {    
 $titlu=$row['titlu'];    
 $categorie=$row['categorie'];    
 $link=$row['link'];    
 $link2=$row['link2'];    
 $hits=$row['hits'];    
 $uploader=$row['uploader'];    
 $data_ad=$row['data_ad'];    
 $tags=$row['tags'];    
 $linkpoza=$row['linkpoza'];    
 $descriere=$row['descriere'];    
 $hits++;    
 }    
 $q="UPDATE carte SET hits='" . $hits . "' WHERE id='" . $id1 . "'";    
 mysql_query($q);

编写的链接就像这样

    http://example.com/carti-Dragoste/ $titlu_$id.html     

来自titlu的空格被替换为 -

我只需要读取网址中的ID ...

由于

1 个答案:

答案 0 :(得分:0)

我认为这可行: $ tetsUrl =“http://example.com/carti-Dragoste/ $ titlu_ $ id.html”; $ idOnlyTmp = array_pop(array_reverse(explode(“。”,$ testUrl)); $ idOnly = array_pop(explode(“_”,$ idOnlyTmp)); echo $ idOnly;