Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

153
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda