0

I have a array from the database which contains links, some values are normal as "index.php" but others look like "browse.php|search.php|calendar.php|tags.php?tags_mode=profile|search.php?show=match", but I only want to use the first path "browse.php" only and trash everything starting from the first "|" inclusively.. how can I do that?

This is the code I have:

$sql = "SELECT * FROM $tablename WHERE `Parent` = '0' AND Type LIKE 'top' ORDER BY `Order` ASC";
    $result = mysql_query($sql) or die ("Error: Query Failed! " .mysql_error());
    $output = "<ul>";
    while ($rs = mysql_fetch_array($result)) {

       $output .= "<li class='menu_top' id='".$rs['ID']."'><a href='".$rs['Link']."'>".$rs['Name']."</a></li>";
       $sql2 = "SELECT * FROM $tablename WHERE `Parent` = '".$rs['ID']."' ORDER BY `Order` ASC";
       $result2 = mysql_query($sql2) or die ("Error: Query Failed! " .mysql_error());
       if (mysql_num_rows($result2) != 0) {
          $output .= "<ul class='menu_sub' id='".$rs['ID']."'>";
          while ($rs2 = mysql_fetch_array($result2)){
             $output .= "<li><a href='".$rs2['Link']."'>".$rs2['Name']."</a></li>";
          }
          $output .= "</ul>";
       }
    }
    $output .="</ul>";
    echo $output;

$rs['Link'] is the one I want to be cut like that..

I tried to subtract but I can't really say I can manage it as I am too new to php.. Thank you for any advice..

4

3 回答 3

2

如果您只需要第一个元素,则可以使用带限制的爆炸:

$link=explode("|",$rs['Link'], 1);
echo $link [0];
于 2013-06-30T21:12:55.463 回答
1

$link=explode("|",$rs['Link']);

$output .= "<li class='menu_top' id='".$rs['ID']."'><a href='".$link[0]."'>".$rs['Name']."</a></li>";

你去吧。

于 2013-06-30T21:08:01.297 回答
0

你可以用纯 sql 做到这一点:

$sql = "SELECT *,IF(INSTR( Link,'|')>0,LEFT( Link,INSTR( Link,'|')-1), Link) AS FormattedLinkFROM $tablename WHERE Parent= '0' AND Type LIKE 'top' ORDER通过 ASC Order";

于 2013-06-30T21:18:54.310 回答