0
public function parseBlade($string, $param = null)
{
    app(\Illuminate\Contracts\View\Factory::class)
        ->share('errors', app(\Illuminate\Support\MessageBag::class));

    extract(app('view')->getShared(), EXTR_SKIP);
    $__env->incrementRender();

    if ($param) {
        extract($param, EXTR_SKIP);
    }
    unset($param);

    ob_start();
    eval('?>' . app('blade.compiler')->compileString($string));
    $content = ltrim(ob_get_clean());

    $__env->decrementRender();
    $__env->flushStateIfDoneRendering();

    return $content;
}

$text = $template->content;
$data = array(
    'token'=>$param['temporaryOrder']['token'],
    'user'=>$param['isUseImei']);
$url = http_build_query($data,'','&');
$obj->setViewData([
    'text' => $this->parseBlade($text,
    [
        'email' => $email, 
        'link' => config('frontend.ec_protocol') . $office->subdomain . config('frontend.ec_url') . '/order/registration?'. $url
    ]),
]);

After calling ob_get_clean(), it changes the & in the link to &. Is there any way to solve this problem?

mickmackusa
  • 43,625
  • 12
  • 83
  • 136
errors
  • 53
  • 4
  • I do not Laravel, but does it auto-encode content being passed to the view so that it is rendered correctly? – mickmackusa Mar 18 '22 at 02:32
  • Are you able to provide a [mcve] by including a string with sample content, then show us what output you are getting, then show us exactly what output you expect? I'd probably rename `$url` to `$querystring` for clarity of code. – mickmackusa Mar 18 '22 at 02:53
  • i just want to convert & into & in the link example: token=uxsrL03fSgXB4iVORdyIq97DhpeZbFaC61kKYnvoAHt5T8PwN2WUjElmzJcQMG&user=1 to : token=uxsrL03fSgXB4iVORdyIq97DhpeZbFaC61kKYnvoAHt5T8PwN2WUjElmzJcQMG&user=1 – errors Mar 18 '22 at 02:57
  • 1
    I know what you are asking is simple, but if Stack Overflow has any hope of receiving correct and informative answers that can be verified as correct, a [mcve] is necessary. When you have this information, [edit] your question; never post question details as a comment. Perhaps this is an XY Problem. Are you trying to fix something that isn't broken? https://stackoverflow.com/a/62055171/2943403 – mickmackusa Mar 18 '22 at 02:58
  • These look related: [Laravel getQueryString() without &](https://stackoverflow.com/q/44402896/2943403), https://laracasts.com/discuss/channels/laravel/blade-being-escaped-to-amp, – mickmackusa Mar 18 '22 at 03:15

2 Answers2

1
$test = '&';

echo htmlspecialchars(htmlspecialchars($test));
Niels
  • 1,005
  • 1
  • 8
  • 18
  • 1
    This answer is missing its educational explanation. Why should the asker (and future researchers) call the same function twice on the input string? – mickmackusa Mar 18 '22 at 02:30
  • @mickmackusa honestly I have no idea why you should call it twice. I just know it works ;) I'm interested in hearing your explanation :) – Niels Mar 18 '22 at 02:44
  • @Niels thanks to you i found the solution! I'm looking for htmlspecialchars_decode() – errors Mar 18 '22 at 02:54
  • At this point, @Niels, my explanation is that the asker is trying to fix something that isn't broken. See my commented link under the question to one of my old answers . – mickmackusa Mar 18 '22 at 03:04
0

When you are passing data to the blade template, {{ $variable }} OR echo inside the view file that will properly encode any characters that should not be placed directly in HTML.

The simplest solution is to disable HTML-encoding: {!! $variable !!}.