简体   繁体   English

如何用“,”符号分隔字符串的各个部分

[英]How to separate parts of strings by “,” sign

UPDATE FOR JAY'S ANSWER : 周杰伦的答案更新

I did what you've done in this link : 我做了您在此链接中所做的事情:

public function ShowTag()
    {
        $blog_tags = array();
        $tag = $this->_db->prepare("SELECT blog_tags FROM blogs");
        $tag->execute();
        while($row = $tag->fetch())
        {
            $blog_tags[] = $row['blog_tags'];
        }
        $final_anchors = "";
        $final_tags = "";
        foreach($blog_tags as $b)
        {
            $make_comma_seprated_tags_to_array = explode(",",$b);
            $final_tags = array_merge($final_tags,$make_comma_seprated_tags_to_array);
        }
        foreach($final_tags as $f)
        {
            $final_anchors .= '<a href="'.$f.'">'.$f."</a>";
        }
        return $final_anchors;
    }

But it gives me these errors: 但这给了我这些错误:

Warning: array_merge(): Argument #1 is not an array 警告: array_merge():参数1不是数组

$final_tags = array_merge($final_tags,$make_comma_seprated_tags_to_array);

Warning: Invalid argument supplied for foreach() 警告:为foreach()提供了无效的参数

foreach($final_tags as $f)

=========================================================================== ================================================== ========================

I'm working with PHP to develop my project. 我正在使用PHP开发我的项目。 Basically I have a table called Blogs which contains some information about blog posts of my site: 基本上,我有一个名为Blogs的表,其中包含有关我的网站的博客文章的一些信息:

capture 捕获

Now I have created a page where all the tags of blog posts are shown there. 现在,我创建了一个页面,其中显示了所有博客文章标签。 So in order to do that, I coded this: 因此,为了做到这一点,我对此进行了编码:

if(!empty($tags)){
    print_r($tagShow);
    $string = implode(',',$tagShow);
    echo $string;
}else{
    echo "There is no tag available right now!";
}

As you can see I'm referring to $tagShow which is basically this method: 如您所见,我指的是基本上是此方法的$tagShow

public function ShowTag()
{
    $blog_tags = array();
    $tag = $this->_db->prepare("SELECT blog_tags FROM blogs");
    $tag->execute();
    while($row = $tag->fetch())
    {
        $blog_tags[] = $row['blog_tags'];
    }
    return $blog_tags;
}

And it returns an array like this: 它返回一个像这样的数组:

Array ( [0] => asdsadsa,hello [1] => new,old ) 

That's why I've used implode function to convert this into string: 这就是为什么我使用implode函数将其转换为字符串的原因:

asdsadsa,hello ,new,old

Now what I want to do is to separate each tag by , sign. 现在,我要做的是用符号分隔每个标签。 Therefore I can put a hyperlink on any of them... 因此,我可以在其中任何一个上放置超链接...

So if you know how to do this, please help me out. 因此,如果您知道该怎么做,请帮帮我。

Thanks in advance. 提前致谢。

public function ShowTag()
{
    $blog_tags = array();
    $tag = $this->_db->prepare("SELECT blog_tags FROM blogs");
    $tag->execute();
    while($row = $tag->fetch())
    {
        $tags = explode(",",$row['blog_tags']);
        $blog_tags=array_merge($blog_tags,$tags);

    }
    // $blog_tags have now all tags
   $final_anchors="";
    foreach($blog_tags as $b)
    {
     $final_anchors.='<a href="BASIC_URL'.$b.'">#'.$b."</a>";
    }
    return $final_anchors;
}

/ We can also imporve code by nested for loop but basics idea will be same / / 我们也可以通过嵌套for循环来改进代码,但是基本思想是相同的 /

http://sandbox.onlinephpfunctions.com/code/5670bf4019292d9cd06402c2fddeed17da9f7449 Check this link for demo http://sandbox.onlinephpfunctions.com/code/5670bf4019292d9cd06402c2fddeed17da9f7449检查此链接以获取演示

You initiated $final_tags as string. 您将$final_tags为字符串。 It must be like this: 必须是这样的:

$final_tags = [];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM