2

I tried to split the characters from the available string, but have not found the right way.

I have a string like below :

$tara = 'wordpress/corporate/business';
$k = explode('/', $tara);
foreach ($k as $l) {
    var_dump(substr_replace($tara, '', $i, count($l)));
}

The result I want is :

'wordpress',
'wordpress/corporate',
'wordpress/corporate/business'

5 Answers 5

4

You could concatenate in every iteration like :

$tara = 'wordpress/corporate/business';
$k = explode('/', $tara);
$result = "";

foreach ($k as $l) {
    $result .= '/'.$l;
    echo $result."<br>";
}

The output will be :

/wordpress
/wordpress/corporate
/wordpress/corporate/business

If you don't need the slashes at the start you could add a condition like :

$tara = 'wordpress/corporate/business';
$k = explode('/', $tara);
$result = "";

foreach ($k as $l) 
{
    if( empty($result) ){
        echo $l."<br>";
        $result = $l;
    }else{
        $result .= '/'.$l;
        echo $result."<br>";
    }
} 

Or using the shortened version inside loop with ternary operation, like :

foreach ($k as $l) 
{
    $result .= empty($result) ? $l : '/'.$l;
    echo $result."<br>";
}

The output will be :

wordpress
wordpress/corporate
wordpress/corporate/business

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

2 Comments

@SiskaBrilian : do you want / in start ?
@SiskaBrilian Please read the placeholder text on the comment boxes under the answers -- Avoid saying "thanks" or "+1" Please delete your comments.
2

How about constructing the results from the array alements:

$tara = 'wordpress/corporate/business';
$k = explode('/', $tara);

$s = '';
foreach ($k as $l) {
    $s .= ($s != '' ? '/' : '') . $l;
    var_dump($s);
}

This results in:

string(9) "wordpress"

string(19) "wordpress/corporate"

string(28) "wordpress/corporate/business"

Comments

1

You should explode your string array and print in foreach all exploded values using implode and temporary array(for example). Here is working code:

$tara = 'wordpress/corporate/business';
$k = explode('/', $tara);
$tmp = [];

foreach ($k as $l) {
    $tmp[] .= $l;
    var_dump(implode($tmp, '/'));
}

Comments

0

Create new array and inside foreach

<?php
$tara = 'wordpress/corporate/business';
$url = array();
foreach(explode("/",$tara) as $val){
  $url[] = $val;
  echo implode("/",$url)."\n";  
}
?>

Demo : https://eval.in/932527

Output is

wordpress
wordpress/corporate
wordpress/corporate/business

Comments

0

Try This, Its Easiest :

$tara = 'wordpress/corporate/business';
$k = explode('/', $tara);
$str = array();
foreach ($k as $l) {
   $str[] = $l;
   echo implode('/',$str)."<br>";
}

3 Comments

Code-only answers are low-value on StackOverflow. Please ALWAYS post an explanation with every answer.
Sure @mickmackusa
...well, are you actually going to fix up your post? Click on edit under your answer.

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.