0

How do I replace the last string after i have loop on each string? I was able to do this but still not complete

// here my get string ?dir=hello1/hello2/hello3

if (isset($_GET['dir'])) {
    $hrefTabs = $_GET['dir']; 
    if (!empty($hrefTabs)) {
        $arrayOfhref = explode('/', $hrefTabs);//I explode string /

        foreach ($arrayOfhref as $roothrefline) {
            $trimstrhref = preg_replace('/\s+/', '', $roothrefline); // Here i trim white space

            $mmhref = '<a href="'.$trimstrhref.'">'.$roothrefline.'</a> / '; // Here i assign each strin hyper reference

            $toreplace = $roothrefline . ' / ';
            $lastobj = substr_replace($mmhref, $toreplace, strlen($mmhref)); // here i want the last element not to have hyper reference attribut

            echo $lastobj; // but i get this at output 
            //<a href="">hello1</a> / hello1 / <a href="">hello2</a> / hello2 /<a href="">hello3</a> / hello3
        }
    }
}

My out put look like this

<a href="">hello1</a> / hello1 / <a href="">hello2</a> / hello2 /<a
href="">hello3</a> / hello3

I want to have it look like this

<a href="">hello1</a> / <a href="">hello2</a> / hello3

2 Answers 2

2

Before your foreach do

$lastElement = array_pop($arrayOfHref);

and display $lastElement just after your loop.

http://php.net/manual/en/function.array-pop.php

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

7 Comments

can you please update the full code more better for others to understand i got this error Warning: array_pop() expects parameter 1 to be array, null given in
@StuartBrian Hey, it's your code, just try to implement it, if you'll succeed, you can always edit your question. The goal of stackoverflow is not to code for someone, but to help one diagnose a problem and find solution. Check if your array is declared before you call array_pop, because it seems that it's not
i i could fix that no need to ask for solution, i just need help on that i have tried everything i could
@StuartBrian paste your new code on pastebin and put the link in comment, I can take a look. pastebin.com
$arrayOfhref and $arrayOfHref are different vars. Please learn php more carefully.
|
0
$dir = "hello1/hello2/hello3";

  if(isset($dir)){
     $hrefTabs = $dir;
     if(!empty($hrefTabs)){
    $arrayOfhref = explode('/', $hrefTabs);//I explode string /
    $i = 0;
    foreach($arrayOfhref as $roothrefline) {
      $trimstrhref = preg_replace('/\s+/', '', $roothrefline); // Here i trim white space
      $mmhref = '<a href="'.$trimstrhref.'">'.$roothrefline.'</a> / '; // Here i assign each strin hyper reference
      $toreplace = $roothrefline . ' / ';

      if(++$i === count($arrayOfhref)){
        echo $roothrefline;
      } else {
        echo $mmhref;
      }
    }
  }

}

Something like this...

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.