如何将单词与“space”分开:(示例1,示例2,...)到(示例1,示例2,...)

时间:2012-08-24 12:16:43

标签: php title space

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

示例: 的 如果我在标题中有这样的单词:“example1,example2,example3,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;
}

?>

4 个答案:

答案 0 :(得分:3)

您可以使用str_replace:

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

答案 1 :(得分:2)

只需用逗号分隔字符串,然后使用逗号和空格作为粘合剂再次将它组合在一起:

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

答案 2 :(得分:0)

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

答案 3 :(得分: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;
}

?>