2015-06-29 29 views
0

我想顯示用戶的個人資料頁(用戶/ UID) 上的代碼,但不包括子目錄/子網站。我做了一個正則表達式,應該返回FALSE在下面的網站:用戶/ uid/forum_topics,但它確實返回TRUE。 (UID =任意數字)的preg_match - 正則表達式來排除子目錄

我的代碼以正則表達式:

$regex_profile = '/\buser\/\d*\/?(?!\d)\b/'; 

if (preg_match($regex_profile, $path)) 
{ 
    //Print Activity on User Profile but exclude children 
    print render($page['profile_activity_area']); 
} 

我不是很熟練的用正則表達式。希望有人能幫助我。提前致謝。

回答

1

如果你想只在具有ADRESS如/用戶/ {ID}

然後用簡單的正則表達式

if(preg_match('#\/user\/\d+#', $path)){ 
    //display code 
} 

正則表達式的頁面顯示的代碼意味着這條道路應該有/用戶/然後位數至少有一次或多次,沒有數字可以在數字之後。

它將匹配

  • /用戶/ 324

但將不匹配

  • /用戶/ 234/ASDF
  • /用戶/一個/ asdfa
  • /user/234/234234/xzcv/a
  • /notuser/234

+0

這並不以某種方式工作。我的路徑如下所示:_user/1_並且它仍然在_user/1/subsite_上顯示代碼 '$ path = current_path(); //路徑:user/ //在用戶配置文件中打印活動,但排除子女 if(preg_match('#user \/\ d +#',$ path)){ \t print render($ page ['profile_activity_area'] ); }' – Oddy

+0

正則表達式是正確的,如果你不想顯示它然後使用!preg_match我不知道你的應用邏輯 – Robert