0

I'm trying to create a link from mysql results:

while($row = $result->fetch_assoc()){
    echo "<a class=post_link href=http://localhost:8888/search.php?loc=".$row['loc']."&lt=".$row['lt']."&lg=".$row['lg'].">".$row['loc'].": ".$row['lt'].", ".$row['lg']."</a><br>" ;
}

The problem is, when 'loc' row contains more than one word. Is it possible to detect when it has two or more words and insert '%' between them, or I need to use different technique?

4

2 回答 2

3

使用 PHP 函数urlencode()。当对要在 URL 的查询部分中使用的字符串进行编码时,此函数很方便。更多信息: http: //php.net/manual/en/function.urlencode.php

while($row = $result->fetch_assoc()){
    echo "<a class=post_link href=http://localhost:8888/search.php?loc=".urlencode($row['loc'])."&lt=".$row['lt']."&lg=".$row['lg'].">".$row['loc'].": ".$row['lt'].", ".$row['lg']."</a><br>" ;
}
于 2013-09-02T18:10:01.903 回答
0

或者你可以使用 ereg_replace,因为 urlencode(据说没有个人经验)有时会有点奇怪:

while($row = $result->fetch_assoc()){
    $loc = ereg_replace("[ \t\n\r]+", "%", $row['loc']);
    echo "<a class=post_link href=http://localhost:8888/search.php?loc=".$loc."&lt=".$row['lt']."&lg=".$row['lg'].">".$row['loc'].": ".$row['lt'].", ".$row['lg']."</a><br>" ;
}

这也替换了换行符和制表符。

于 2013-09-02T18:20:54.613 回答