0

我在带有标题输出的 php 代码中遇到问题。

示例: 如果我在标题中有这样的词: “example1,example2,example3,example4,...”这个标题扩展了我的云 div 样式。

输出.$row['title']。应该用空格分隔从 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;
}

?>
4

4 回答 4

3

您可以为此使用 str_replace:

echo str_replace(",", ", ", $the_string);
于 2012-08-24T12:19:03.110 回答
2

只需用逗号分隔字符串,然后使用逗号和空格作为胶水将其重新组合在一起:

 $row['title'] = implode( ', ', explode( ',', .$row['title'] ) );
于 2012-08-24T12:18:46.683 回答
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;
}

?>
于 2012-08-24T12:35:38.343 回答
0
    $txt = $row['title'];   
    $txt = explode(",", $txt);
    foreach($txt as $key => $value) {
       echo $value.", ";
    }
于 2012-08-24T12:20:57.833 回答