12

The string I am trying to split is $item['category_names'] which for example contains Hair, Fashion, News

I currently have the following code:

$cats = explode(", ", $item['category_names']);
foreach($cats as $cat) {
    $categories = "<category>" . $cat . "</category>\n";
}

I want the outcome of $categories to be like the following so I can echo it out later somewhere.

<category>Hair</category>\n
<category>Fashion</category>\n
<category>News</category>\n

Not sure if I am going the right way about this?

5
  • 3
    does it work? That's a pretty good smoke test. if you do $categories .= it would at least do what you appear to be expecting. Commented May 18, 2012 at 10:31
  • 3
    $categories .= // concatenation missing Commented May 18, 2012 at 10:32
  • Did you try this code? Should work I think Commented May 18, 2012 at 10:32
  • 3
    I would explode on , and then trim the result, not explode on , Commented May 18, 2012 at 10:32
  • 1
    Perhaps you might be interested in using the simpleXML object. It's a fairly simple and sturdy way of creating xml files Commented May 18, 2012 at 10:33

5 Answers 5

31

In your code you are overwritting the $categories variable in each iteration. The correct code would look like:

$categories = '';
$cats = explode(",", $item['category_names']);
foreach($cats as $cat) {
    $cat = trim($cat);
    $categories .= "<category>" . $cat . "</category>\n";
}

update: as @Nanne suggested, explode only on ','

Sign up to request clarification or add additional context in comments.

2 Comments

@AD7six nobody states that this is the only solution, it's actually fixed version of the OP's code; so yeah, it's the correct version of what OP's implementation.
except "what's wrong with my code" isn't the question that has been asked
2

Without a for loop

$item['category_names'] = "Hair,   Fashion,   News";
$categories = "<category>".
        implode("</category>\n<category>", 
        array_map('trim', explode(",", $item['category_names']))) . 
        "</category>\n";
echo $categories;

3 Comments

This doesn't simplify anything and array_map('trim', $cats) is a lot simpler (and efficient) than using array_walk and create function
array_map is what I was looking for earlier! Thanks.
much better :) - formatting so it fits in the code window (or not using multiple php functions on one line) would make it easier to read
2

PHP explode array by loop demo: http://sandbox.onlinephpfunctions.com/code/086402c33678fe20c4fbae6f2f5c18e77cb3fbc2

its works for me

<?php

$str = "name:john,hone:12345,ebsite:www.23.com";

$array=explode(",",$str);

if(count($array)!=0)
{
foreach($array as $value)
{

    $data=explode(":",$value);

      echo $data[0]."=".$data[1];


    echo ' ';

}
}
?>

2 Comments

@Pierre.Vriens what do you want can u plz explain it
posting code with no explanation is perceived as low quality. You should also explain why/how the code you posted does actually answer the question being asked. E.g.: "part ... in the code so and so (as included in the question) contains an error. I.e. this and this is wrong. Instead it should be corrected like this ... This actually solves your error because ....". PS: "read the source" is not high quality either.
1

if you use this:

$cats = explode(", ", $item['category_names']);
foreach($cats as $cat) {
$categories = "<category>" . $cat . "</category>\n";
}

the $categories string is overwritten each time, so "hair" and "fasion" are lost..

if you however add a dot before the equal sign in the for loop, like so:

$cats = explode(", ", $item['category_names']);
foreach($cats as $cat) {
$categories .= "<category>" . $cat . "</category>\n";
}

the $catergories string will consist of all three values :)

Comments

0

The error in your code is this:

$categories = "<category>" . $cat . "</category>\n";

You are overwriting $categories at each iteration, it should be:

$categories .= "<category>" . $cat . "</category>\n";

Not sure if I am going the right way about this?

find and replace isn't what explode is for. If you just want to correct the code error - see above.

This is more efficient:

$categories = "<category>" .
    str_replace(', ', "</category>\n<category>", $input) . 
    "</category>\n";

And this also accounts for variable whitespace:

$categories = "<category>" . 
    preg_replace('@\s*,\s*@', "</category>\n<category>", $input) . 
    "</category>\n";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.