2012-08-24 30 views
0

我有PHP代碼與標題輸出問題。如何從「空格」分開的話:(例1,例2,...)到(例1,例2,...)

例子: 如果我有這樣的標題詞:「例1,例2,示例3,範例4,......」這個稱號擴大我的雲格的風格。

輸出。$行[ '標題']。 768,16 獨立話從例1,例2,示例3至例1,例2,示例3,......有空格,但事實並非如此。如何改變這個會導致這個問題的代碼?

感謝的。

<?php 


function print_cloud() 
{ global $use_ads_scrl; $res=""; if ($use_ads_scrl=="yes"){$res=print_cloud2();} return $res; } 

function print_cloud2() 
{ 

global $table_ads, $HTTP_GET_VARS; 

$city_sch=""; 
if ($HTTP_GET_VARS['city']!=""){$city_sch="and city='".$HTTP_GET_VARS['city']."' ";} 

$sql_query="select * from $table_ads where (adcommkey is null or adcommkey=0) and visible=1 $city_sch 
order by RAND() limit 10"; 

$sql_res=mysql_query("$sql_query"); 

$min = '10'; // Minimum font size in pixel. 
$max = '22'; // Maximum font size in pixel. 

$k1=""; $html_res=""; 
while ($row = mysql_fetch_array($sql_res)){ 
$k1="1"; 
if($row['adphotos']=='yes'){$check_ph=$photo_mark;} else {$check_ph="";} 

$html_res=$html_res." 
<a style='font-size:".rand($min,$max)."px; font-family:tahoma,sans-serif;'  
    class=\"tag_cloud\" 
     href='index.php?md=details&id= ".$row['idnum']." '> ".$row['title']." </a> 
"; 


} 

$html_res=" 
$html_res 
"; 

if ($k1==""){$html_res="";} 

return $html_res; 
} 

?> 
+1

你確定你發佈正確的代碼? – Peon

回答

2

用逗號就分割字符串,並把它再次一起使用逗號和膠水的空間:

$row['title'] = implode(', ', explode(',', .$row['title'])); 
+0

這比str_replace慢得多。 – Daniel

+0

啊,我看:http://micro-optimization.com/str_replace-vs-implode-explode – feeela

3

您可以使用str_replace函數此:

echo str_replace(",", ", ", $the_string); 
+0

非常感謝你非常。 :) – Sela

0
$txt = $row['title']; 
    $txt = explode(",", $txt); 
    foreach($txt as $key => $value) { 
     echo $value.", "; 
    } 
0

我的答案似乎太簡單了,但沒有錯。

更改此代碼:

$html_res=$html_res." 
<a style='font-size:".rand($min,$max)."px; font-family:tahoma,sans-serif;'  
    class=\"tag_cloud\" 
     href='index.php?md=details&id= ".$row['idnum']." '> ".$row['title']." </a> 
"; 

這個代碼:

$html_res=$html_res." 
<a style='font-size:".rand($min,$max)."px; font-family:tahoma,sans-serif;'  
    class=\"tag_cloud\" 
     href='index.php?md=details&id= ".$row['idnum']." '> ".$row['title'].",</a> "; 

更新全代碼如下:

<?php 


function print_cloud() 
{ global $use_ads_scrl; $res=""; if ($use_ads_scrl=="yes"){$res=print_cloud2();} return $res; } 

function print_cloud2() 
{ 

global $table_ads, $HTTP_GET_VARS; 

$city_sch=""; 
if ($HTTP_GET_VARS['city']!=""){$city_sch="and city='".$HTTP_GET_VARS['city']."' ";} 

$sql_query="select * from $table_ads where (adcommkey is null or adcommkey=0) and visible=1 $city_sch 
order by RAND() limit 10"; 

$sql_res=mysql_query("$sql_query"); 

$min = '10'; // Minimum font size in pixel. 
$max = '22'; // Maximum font size in pixel. 

$k1=""; $html_res=""; 
while ($row = mysql_fetch_array($sql_res)){ 
$k1="1"; 
if($row['adphotos']=='yes'){$check_ph=$photo_mark;} else {$check_ph="";} 

$html_res=$html_res." 
<a style='font-size:".rand($min,$max)."px; font-family:tahoma,sans-serif;'  
    class=\"tag_cloud\" 
     href='index.php?md=details&id= ".$row['idnum']." '> ".$row['title'].",</a> "; 


} 

$html_res=" 
$html_res 
"; 

if ($k1==""){$html_res="";} 

return $html_res; 
} 

?> 
+0

嗨,你只是廣告(,)在2002年底($行[「標題」]。「),但問題是,輸出$行[‘標題’已是完整的句子,例如TEST1 ,test2,test3,...感謝您的幫助 – Sela

+0

我明白了,然後feeela的解決方案似乎是合適的。 – Fatih

相關問題