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

428
Vistas
Regex (PHP) to extract a sentence that contains a link

I want to retrieve the entire sentence that surrounds a link, delimited by punctuation (such as . or ! or ? or newline).

The purpose is to provide a better context for the link.

So for example if i have this...

$input = "I don't want this piece! This is the <a href='https://example.com/my-sentence'>sentence</a> I want. In don't want this piece either";
$filter = "https://example.com/my-sentence";

... I need to get to that...

$output = "This is the sentence I want.";

So far, I managed to isolate a sentence that doesn't contain tags, like this:

$input = "I don't want this piece. This is the sentence I want. In don't want this piece either";
$filter = "sentence";
$regex = '/[A-Z][^\\.;]*('.$filter.')[^\\.;]*/';
if (preg_match($regex, $input, $match))
$output = $match[0];

This works just fine. Next, I don't know how to get around the punctuation inside the url.

I explored isolating the anchor first and regexing that, which works on any single example but may generate collisions in the wild (anchors duplicating other anchors or random text).

Another way to go seems to be strip_tags, something like...

$input = strip_tags($input);

... the problem being that I need them both stripped and not stripped at the same time.

Maybe a more specific regex or some smart wrapping of the functions could bring an easy way out of this, or maybe it's a dead end and some other approach is required, I don't know, but right now I'm stuck, please help!

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

Granted you do not care about abbreviations, you can match either a char other than ?, ! and ., or a link-like substring any zero or more times before and after a specific filter string:

$input = "I don't want this piece! This is the <a href='https://example.com/my-sentence'>sentence</a> I want. In don't want this piece either";
$filter = "sentence";
$regex = '~\b(?:[^.?!]|https?://[^<>\s"\']++)*?'.preg_quote($filter, '~').'(?:[^.?!]|https?://[^<>\s"\']++)*~u';
if (preg_match_all($regex, $input, $match)){
  print_r( array_map(function($x) {return strip_tags($x);}, $match[0]) );
}

See the PHP demo. Output:

Array
(
    [0] => This is the sentence I want
)

See the regex demo. Details:

  • \b - a word boundary
  • (?:[^.?!]|https?://[^<>\s"\']++)*? - zero or more occurrences, as few as possible, of either a char other than ., ? and ! or http, an optional s, :// and then one or more chars other than <, >, whitespace, ", '
  • sentence - a filter string
  • (?:[^.?!]|https?://[^<>\s"\']++)* - zero or more occurrences, as many as possible, of either a char other than ., ? and ! or http, an optional s, :// and then one or more chars other than <, >, whitespace, ", '
over 4 years ago · Santiago Trujillo 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