【问题标题】:Separate string without comma in PHP [duplicate]PHP中没有逗号的分隔字符串[重复]
【发布时间】:2014-02-15 10:29:37
【问题描述】:

我原来的字符串是

one,two,three,four,five,

我需要将每个单词作为链接分开

<a href="">one</a>, <a href="">two</a>, <a href="">three</a>, ...

我的代码是

$plat = $row['reg'];
foreach ($plat as $key => $pv) {
    $pl[] = implode(',', $pv);
}
for ($p = 1; $p = sizeof($pl); $i++) {
    echo '<a href="#" rel="tag">' . $pl[i] . '</a>';
}

【问题讨论】:

  • 简单地使用explode(",",$str)在数组中用逗号分割字符串并根据需要使用它
  • @SatishSharma 不仅需要像我的代码一样将第二个交互器放入第一个。

标签: php arrays string function


【解决方案1】:

这就够了..

<?php
$str='one,two,three,four,five';
$arr=explode(',',$str);
foreach($arr as $val)
{
echo "<a href=''>$val</a>, ";
}

OUTPUT :

<a href=''>one</a>, <a href=''>two</a>, <a href=''>three</a>, <a href=''>four</a>, <a href=''>five</a>

【讨论】:

    【解决方案2】:
       <?php
         $str='one two three four five';
          $arr=explode(' ',$str);
          foreach($arr as $val)
            {
             echo "<a href=''>$val</a>, ";
             }
            this Code to separate blank spacesepration 
    

    【讨论】:

      【解决方案3】:
      $string = 'one,two,three,four,five';
      $tag_open = '<a href="#" rel="tag">';
      $tag_close = '</a>';
      echo $tag_open. implode($tag_close.', '.$tag_open, explode(',', $string)). $tag_close;
      

      【讨论】:

        【解决方案4】:

        试试这个代码:

        $plat=$row['reg'];
        foreach ($plat as $key=> $pv) {
            $pl[] = explode(',', $pv);
            for($p=1;$p=sizeof($pl); $i++) {
                echo '<a href="#" rel="tag">'.trim($pl[i]).'</a>';
            }
        }
        

        【讨论】:

          猜你喜欢
          • 2015-08-11
          • 1970-01-01
          • 2023-04-09
          • 1970-01-01
          • 2012-08-12
          • 2016-04-19
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多