Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

150
Visualizações
replace url into anchor tag with using nl2br in laravel blade

hello i have laravel blade and here is the part that have the issue

@php
$reg_exUrl = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/";
@endphp

<div class="message"> 
                        @php
$text = nl2br(e($message));
@endphp
@if(preg_match($reg_exUrl, $text, $url)){!! preg_replace($reg_exUrl, '<a href="$0" target="_blank">$0</a> ', $text) !!}
@else {!!$text!!}
@endif
</div>

this works good if the link is only in the text but if it got mixed with some words and some break line like this message

hello dear buyer

the link is working good https://google.com/blabla

and some random text

it converted to this html code

hello dear buyer<br>
<br>
the link is working good<br>
<a href="https://google.com/blabla<br" target="_blank">https://google.com/blabla<br< a="">  /&gt;
<br>
and some random text<br>
<br>

how to fix this

i want to replace the url into anchor tag and show the break line if the user enters message contains lines

also to use the e() function in blade to prevent any html code from running

any one help please

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Hmm, your code just works if there is only a space before the <br> tag. I don't think you can do this since the
is tied to link. Maybe you can do this for now (until someone else has a better solution) and make sure the e() is called after the str_replace:

$message = str_replace('<br>', ' <br>', $message);

// Optional to trim double spaces
$message = preg_replace('/\s\s+/', ' ', $message);
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda