包装字符串PHP

时间:2016-11-25 03:08:27

标签: php json

我的代码有问题,我有这个代码可以从外部图像源创建图像&串。我用json来获取字符串。

我的问题是,如果我使用json数据中的字符串,我无法正确包装字符串,如下所示:

http://prntscr.com/dbhg4n

            $url = 'https://bible-api.com/Psalm100:4-5?translation=kjv';

            $JSON = file_get_contents($url);
            $data = json_decode($JSON);
            $string  = $data->text;

但是,如果我直接声明并设置字符串,我得到了我想要的输出:

http://prntscr.com/dbhg7q

$string = "Enter into his gates with thanksgiving, and into his courts with praise: be thankful unto him, and bless his name. For the Lord is good; his mercy is everlasting; and his truth endureth to all generations.";

我不认为错误或问题出现在我的图像上包装文本的代码上。我认为这是关于json的数据。我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

text\n个符号。只需更换它们:

$string = preg_replace("/\n/", ' ', $data->text);

或没有正则表达式:

$string = str_replace("\n", ' ', $data->text);