Preg_match c/ Regular expression

Asked

Viewed 277 times

0

I need to remove everything around the link below. I’m using preg_match with regular expression, and as I’m new in the area, I’m having a hard time finding my mistake. Would you please help me?

Like I’m doing:

preg_match('/www.facebook.com/plugins/video.php?href=\/(.+)&show_text=0&width=560', $v, $output

Full link: https://www.facebook.com/plugins/video.php?href=https%3A%2F%2Fwww.facebook.com%2Fportateste%2Fvideos%2F1585186298555265%2F&show_text=0&width=560

Accurate: "https%3A%2F%2Fwww.facebook.com%2Fportatest%2Fvideos%2F1585186298555265%2F"

Thank you

  • 1

    You can try to get what is in the group of this regex: href=(.+)(?=&show_text), in which here is the demo. As I have no experience in php, I don’t know the programming. But this regex captures the group that is between href= and &show_text

1 answer

1

You can use the following expression:

href=(.*?)&

That way he’ll capture everything in between href, until the first &.

Example:

<?php

preg_match("/href=(.*?)&/", $link, $result);

var_dump( $result[1] );

Demonstration

If you want something more complex, just use the expression below:

href=(.*?)(?(?=&)&|$)
     └─┬─┘ └┬─┘└┬┘└┬┘
       │    │   │  └── Caso não haja, selecione até o último caractere.
       │    │   └───── Caso haja, seleciona até ele.
       │    └───────── Verifique se há `&` na URL
       └────────────── Captura tudo após o `href=`

Demonstration

Browser other questions tagged

You are not signed in. Login or sign up in order to post.