2012-08-24 1 views
0

제목 출력으로 PHP 코드에 문제가 있습니다."space"로 단어를 분리하는 방법 : (example1, example2, ...) ~ (example1, example2, ...)

예 : 이 같은 제목의 단어가있는 경우 : "예 1, 예 2, 예 3, Example4를 ..."이 제목 내 클라우드 DIV 스타일을 확장합니다.

출력 . $ row [ 'title']. shoud 단어가 example1, example2, example3에서 example1, example2, example3 ...과 공백으로 구분되어 있지만 단어가 없습니다. 이 코드를 변경하면 어떻게 될까요?

감사합니다.

<?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

안녕하세요, (. $ 행 [ '제목']. ")의 끝에 (,) 광고하지만 문제는 출력 $ 행 [ '제목'은 이미 완전한 문장입니다. 예를 들어, test1 , test2, test3, ... 도와 줘서 고마워. – Sela

+0

필자의 솔루션이 적절한 것 같다. – Fatih