Search code examples
phpcodeigniterexplode

How to Explode the string in Codeigniter


I want to explore the following string which I am fetching from the database in CodeIgniter using a foreach loop. Actually, I am using a double delimiter to differentiate tags like SQL and MySQL because LIKE "%sql%" will return MySQL results as well. Should be LIKE "%|sql|%"

|login||background||signup||material-design|

I tried the following code but no success.

$snippettag_id = explode('|', $snippet_tags);

    foreach($snippettag_id as $tag_id) {
    $tag_name = $tag_id;
    $this->db->or_like('snippets_name', $tag_name);
    }

Solution

  • Your issue is with same delimiter and symbol in text, Here you need to replace your delimiter in db and in explode(), Try to save , instead of | as delimiter

    $str = "|sql|,|Mysql|,|php|,|C#|,|C++|";//DB string
    echo'<pre>';echo $str;
    $strArray = explode(',', $str);
    echo'<pre>';print_r($strArray);die;
    

    Output:

    |sql|,|Mysql|,|php|,|C#|,|C++|
    
    Array
    (
        [0] => |sql|
        [1] => |Mysql|
        [2] => |php|
        [3] => |C#|
        [4] => |C++|
    )
    
    

    Apart from solution

    you should probably normalize your database it will more reliable and easy to do searching in future