在php中查找字符串后替换最后一个元素字符串

在php中查找字符串后替换最后一个元素字符串,php,replace,Php,Replace,在每个字符串上有循环后,如何替换最后一个字符串? 我能够做到这一点,但仍然没有完成 // here my get string ?dir=hello1/hello2/hello3 if (isset($_GET['dir'])) { $hrefTabs = $_GET['dir']; if (!empty($hrefTabs)) { $arrayOfhref = explode('/', $hrefTabs);//I explode string /

在每个字符串上有循环后,如何替换最后一个字符串? 我能够做到这一点,但仍然没有完成

// 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
        }
    }
}
//这里是我的获取字符串?dir=hello1/hello2/hello3
如果(isset($\u GET['dir'])){
$hrefTabs=$_GET['dir'];
如果(!空($hrefTabs)){
$arrayOfhref=explode('/',$hrefTabs);//I explode字符串/
foreach($ArrayOfRef作为$roothrefline){
$trimstrhref=preg_replace('/\s+/','$roothrefline);//这里我修剪空白
$mmhref='/';//这里我分配每个strin超引用
$toreplace=$roothrefline'/';
$lastobj=substr_replace($mmhref,$toreplace,strlen($mmhref));//这里我希望最后一个元素不具有超引用属性
echo$lastobj;//但我在输出时得到这个
///hello1//hello2//hello3
}
}
}
我的头发看起来像这样

<a href="">hello1</a> / hello1 / <a href="">hello2</a> / hello2 /<a
href="">hello3</a> / hello3
<a href="">hello1</a> / <a href="">hello2</a> / hello3
/hello1//hello2//hello3
我想让它看起来像这样

<a href="">hello1</a> / hello1 / <a href="">hello2</a> / hello2 /<a
href="">hello3</a> / hello3
<a href="">hello1</a> / <a href="">hello2</a> / hello3
//hello3
在您的foreach do之前

$lastElement = array_pop($arrayOfHref);
并在循环之后显示$lastElement

在您开始之前

$lastElement = array_pop($arrayOfHref);
并在循环之后显示$lastElement

$dir=“hello1/hello2/hello3”;
if(isset($dir)){
$hrefTabs=$dir;
如果(!空($hrefTabs)){
$arrayOfhref=explode('/',$hrefTabs);//I explode字符串/
$i=0;
foreach($ArrayOfRef作为$roothrefline){
$trimstrhref=preg_replace('/\s+/','$roothrefline);//这里我修剪空白
$mmhref='/';//这里我分配每个strin超引用
$toreplace=$roothrefline'/';
如果(++$i===计数($ArrayOfRef)){
echo$roothrefline;
}否则{
echo$mmhref;
}
}
}
}

类似这样的东西…

$dir=“hello1/hello2/hello3”;
if(isset($dir)){
$hrefTabs=$dir;
如果(!空($hrefTabs)){
$arrayOfhref=explode('/',$hrefTabs);//I explode字符串/
$i=0;
foreach($ArrayOfRef作为$roothrefline){
$trimstrhref=preg_replace('/\s+/','$roothrefline);//这里我修剪空白
$mmhref='/';//这里我分配每个strin超引用
$toreplace=$roothrefline'/';
如果(++$i===计数($ArrayOfRef)){
echo$roothrefline;
}否则{
echo$mmhref;
}
}
}
}


类似这样的东西…

你能更新完整的代码让其他人更好地理解我得到了这个错误
警告:array_pop()希望参数1是array,null在
@StuartBrian中给出嘿,这是你的代码,只要试着实现它,如果你成功,你可以随时编辑你的问题。stackoverflow的目标不是为某人编写代码,而是帮助某人诊断问题并找到解决方案。在调用array_pop之前,请检查您的数组是否已声明,因为它似乎不是我可以解决的问题,无需寻求解决方案,我只需要帮助,我已经尝试了所有我尝试过的方法could@StuartBrian将你的新代码粘贴到pastebin上,并将链接放在注释中,我可以看一下<代码>$ArrayOfRef
$ArrayOfRef
是不同的变量。请更仔细地学习php。能否请您更新完整的代码,以便其他人更好地理解我遇到的错误
警告:array_pop()希望参数1为array,null在
@StuartBrian中给出嘿,这是您的代码,请尝试实现它,如果您成功,您可以随时编辑您的问题。stackoverflow的目标不是为某人编写代码,而是帮助某人诊断问题并找到解决方案。在调用array_pop之前,请检查您的数组是否已声明,因为它似乎不是我可以解决的问题,无需寻求解决方案,我只需要帮助,我已经尝试了所有我尝试过的方法could@StuartBrian将你的新代码粘贴到pastebin上,并将链接放在注释中,我可以看一下<代码>$ArrayOfRef
$ArrayOfRef
是不同的变量。请仔细学习php。