Werkstudent1: json_encode für komplexe objekte

Beitrag lesen

Hi,

Ich möchte einen json an ajax schicken. Mit den Optionen habe ich auch etwas gebastelt, aber irgendwas sinnvolles kam da auch nicht bei raus. Ich habe mir jetzt mit Strings mein eigenes json gebaut. sieht jetzt deutlich besser aus. Danke.

Gibt es eigentlich eine elegantere Variante ohne eine Million str_replace die html tags eines textes wegzukriegen?

  
        $json_string="{";
        $json_index = 0;
        if (mysqli_num_rows($result) > 0) {
            // output data of each row
            while($row = mysqli_fetch_assoc($result)) {
                $content= $row["post_content"];
                $content= str_replace("<img", "", $content );
                $content= str_replace("/>", "", $content );

                $content= str_replace("<li", "", $content );
                $content= str_replace("<h1>", "", $content );
                $content= str_replace("<h2>", "", $content );
                $content= str_replace("</h2>", "", $content );
                $content= str_replace("<blockquote>", "", $content );
                $content= str_replace("</blockquote>", "", $content );
                $content= str_replace("\n", "", $content );
                $content= str_replace("\r", "", $content );
                $content= str_replace("\t", "", $content );
                $content= str_replace('"', "'", $content );
                $content= str_replace(">", "", $content );
                $content= str_replace("<", "", $content );
                $content= str_replace("/", "", $content );

                $arr = explode(".", $content);
                $short="";
                $length = sizeof($arr);
                for ($i = 0; $i <= $length; $i++) {
                    if(strlen($short) < 300){
                        $short= $short . $arr[$i] . ".";
                    }
                }
                // Ab hier geht etwas bei der umwandlung in json schief
                $json_string = $json_string . '"'. $json_index .'":{ "title":"' . $row["post_title"] .'", "content":"' . $short .'"},';
                $json_index++;
            }
        } else {
            echo "0 results";
        }

        $json_string = substr($json_string, 0, -1);
        $json_string = $json_string . "}";
        echo $json_string;

        mysqli_close($conn);
        ?>

Beste Grüßen

Werkstudent1