Preg_Match w / regular expression

0

I need to remove everything around the link below. I'm using preg_match with regular expression, and since I'm new to the area, I'm having trouble finding my error. Could you please help me out?

How do I do:

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

Full link: link

I need to: "https% 3A% 2F% 2Fwww.facebook.com% 2Fportateste% 2Fvideos% 2F1585186298555265% 2F"

Thank you

    
asked by anonymous 22.02.2018 / 19:32

1 answer

1

You can use the following expression:

href=(.*?)&

In this way it will capture everything from href to first & .

Example:

<?php

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

var_dump( $result[1] );

Demo

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='

Demo

    
22.02.2018 / 20:31