user3469
user3469

Reputation: 23

How to use php to explode string, remove extra characters

I've been using PHP for a while and came across an issue where turning a string into an array (explode), and then simply removing system output characters around each element was not easy to achieve.

I have a string of categories where the output needs to be handled:

2;#Ecosystems;#3;#Core Science Systems (CSS);#4;#Energy and Minerals;#5;#Director

I have tried this particular code below:

$text = "2;#Ecosystems;#3;#Core Science Systems (CSS);#4;#Energy and Minerals;#5;#Director";
$explode = explode('#' ,  $text);
foreach ($explode as $key => $value)
{
    ob_start();
    echo ''.$value.''; 
    $myStr = ob_get_contents();
    ob_end_clean();

}
echo "$myStr"

The only element returned is:

 Director

Can someone point me in the right direction. I'd like to view the string like this:

Ecosystems; Core Science Systems (CSS); Energy and Minerals; Director

Upvotes: 2

Views: 2523

Answers (6)

David Petersen
David Petersen

Reputation: 29

this solves the problem, you can fine-tune the output:

$text = "2;#Ecosystems;#3;#Core Science Systems (CSS);#4;#Energy and Minerals;#5;#Director";
$explode = explode('#' ,  $text);
$myStr = '';
foreach ($explode as $key => $value)
{
  $myStr .= $value;
}
echo "$myStr"

Upvotes: 0

rjdown
rjdown

Reputation: 9227

$myStr = ob_get_contents();

This will replace $myStr. What you're probably looking to do is add to it:

$myStr .= ob_get_contents();

Though looking at your required output, your code is not going to achieve what you want, because you're not removing the preceding part e.g. #1;.

Try this

$text = "2;#Ecosystems;#3;#Core Science Systems (CSS);#4;#Energy and Minerals;#5;#Director";
$text = preg_replace('/(#?[0-9];#)/', ' ', $text);
echo $text;

Output: Ecosystems; Core Science Systems (CSS); Energy and Minerals; Director

Upvotes: 1

AbraCadaver
AbraCadaver

Reputation: 78994

I would use preg_split and do it like this:

$result = implode(' ', preg_split('/#?\d+;#/', $text, null, PREG_SPLIT_NO_EMPTY));

Upvotes: 0

NaijaProgrammer
NaijaProgrammer

Reputation: 2957

The reason is that you place both ob_start() and ob_end_clean() inside the loop. Place them outside the loop:

ob_start();
foreach ($explode as $key => $value)
{
   echo ''.$value.''; 
   $myStr = ob_get_contents();
}
ob_end_clean();
echo "$myStr";

Upvotes: 0

h00ligan
h00ligan

Reputation: 1481

For this exact example you give you could change the explode to split on ;#

$text = "2;#Ecosystems;#3;#Core Science Systems (CSS);#4;#Energy and Minerals;#5;#Director";
$categories = array();
$explode = explode(';#' ,  $text);
foreach ($explode as $key => $value)
{
    if (!is_numeric($value)) {
        $categories[] = $value;
    }
}
echo implode("; ", $categories);

Upvotes: 1

Estus Flask
Estus Flask

Reputation: 222820

Your problem is not question-related, you overwrite your buffer with each foreach iteration, the fix is

$myStr .= ob_get_contents();

Upvotes: 1

Related Questions