简体   繁体   English

str_replace()无法正常工作

[英]str_replace() not working correctly

I've been having this issue for a few hours, and I can't seem to figure out what it could be. 我已经有几个小时的问题了,我似乎无法弄清楚它可能是什么。

I currently have an Administration Panel script that allows Admins to post blogs on the main website, and I have a line that is utilizing str_replace(); 我目前有一个管理面板脚本,该脚本允许管理员在主网站上发布博客,并且我有一行正在使用str_replace();。

foreach($_FILES["images"]["tmp_name"] AS $index => $tmpName)
{
     //replace in body
     $Body = str_replace("[" . $index + 1 . "]", "<img src=\"/images/gallery/" . $seoTitle . "/" . $_FILES["images"]["name"][$index] . "\" />", $Body);

Currently, when writing a blog post, I allow images to be uploaded, and while the script is processing each image, it'll replace a tag such as [1] [2] (it's image index + 1) with the actual img tag. 目前,在撰写博客文章时,我允许上传图片,并且在脚本处理每张图片时,它将使用实际的img标签替换诸如[1] [2](即图片索引+ 1)之类的标签。

I had the script output the parsed Body and it returned something around 我让脚本输出了已解析的正文,它返回了一些内容

This is body content [<img src="/images/gallery/asdasdasdasd/windows.png">

As you can see, the image has a open bracket right before the image tag starts for an odd reason. 如您所见,由于奇怪的原因,图像在图像标签开始之前正好有一个开括号。

I haven't been able to find a solution for some reason. 由于某种原因,我无法找到解决方案。 Has this ever happened to anyone else? 这有没有发生过?

Thanks, Jacob 谢谢,雅各布

As Dave commented that the problem could be within another part of the code, the entire POST process is below: 正如Dave所说的那样,问题可能出在代码的另一部分,整个POST过程如下:

if(isset($_POST["create"]))
{
    $Title = trim($_POST["title"]);
    $Body = trim($_POST["body"]);
    $seoTitle = seo($Title);
    if($Title == "" || $Body == "")
    {
        $Template->assign_var("error", "Dude, fill in all of the fields.");
    }
        else
    {
        if(!empty($_FILES["images"]))
        {
            $allowed_types = array("image/gif", "image/jpeg", "image/jpg", "image/pjpeg", "image/x-png", "image/png");
            $allowed_ext = array("jpg", "jpeg", "gif", "png");
            $photos = array();

            if(!is_dir(ROOT_DIR . "../images/gallery/" . $seoTitle))
                mkdir(ROOT_DIR . "../images/gallery/" . $seoTitle);
            foreach($_FILES["images"]["tmp_name"] AS $index => $tmpName)
            {
                $filedata = explode(".", $_FILES["images"]["name"][$index]);
                $ext = end($filedata);
                if(in_array($ext, $allowed_ext) && in_array($_FILES["images"]["type"][$index], $allowed_types))
                {
                    move_uploaded_file($tmpName, ROOT_DIR . "../images/gallery/" . $seoTitle . "/" . $_FILES["images"]["name"][$index]);
                    //replace photo in body
                    $Body = str_replace("[" . $index + 1 . "]", "<img class=\"blog_photo\" src=\"/images/gallery/" . $seoTitle . "/" . $_FILES["images"]["name"][$index] . "\" />", $Body);
                    $photos[] = $_FILES["images"]["name"][$index];
                }
            }
            $Template->assign_var("error", $Body);
        }
    }
}

use this to findout where is the problem and compare output 用它来找出问题所在并比较输出

print_r($_FILES["images"]["tmp_name"]);
echo "<br />";

foreach($_FILES["images"]["tmp_name"] AS $index => $tmpName)
{
     //replace in body
     $Body = str_replace("[" . $index + 1 . "]", "<img src=\"/images/gallery/" . $seoTitle . "/" . $_FILES["images"]["name"][$index] . "\" />", $Body);

    echo "[" . $index + 1 . "]" . "<br />";
    echo "<img src=\"/images/gallery/" . $seoTitle . "/" . $_FILES["images"]["name"][$index] . "\" />";
}

Evidently, the issue is indeed strange, thought it is made due to parentheses not being used when doing $index + 1. 显然,这个问题确实很奇怪,认为这是由于在执行$ index + 1时未使用括号引起的。

So changing the code to the following: 因此,将代码更改为以下内容:

$Body = str_replace("[" . ($index + 1) . "]", "<img class=\"blog_photo\" src=\"/images/gallery/" . $seoTitle . "/" . $_FILES["images"]["name"][$index] . "\" />", $Body);

will indeed work. 确实会工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM