0

i want to match a url inside anchor html and return the url. the sub-domain and path file always change.

this's the url.

https://regex101.com/r/gB4bB8/3

i want it to return the url only https://s04.solidfilesusercontent.com/NGU0MGIwZTg1MDU0MjI5YzE3YzM3NDQ4ZWJhNjVhN2Y3ZGVmN2EwNDoxYkFmRkY6ODZvb29qRG8tdFluMXl2TU1hR1dBN19ockt3/v8grz2RpkMkWd/As_The_Gods_Will_%282014%29-1.mp4

this's my code so far

$matches = "";

$regex = '/<a.+?href=("|\')(.+?)\1/i';

$url = "<a target='blank' href='https://s04.solidfilesusercontent.com/NGU0MGIwZTg1MDU0MjI5YzE3YzM3NDQ4ZWJhNjVhN2Y3ZGVmN2EwNDoxYkFmRkY6ODZvb29qRG8tdFluMXl2TU1hR1dBN19ockt3/v8grz2RpkMkWd/As_The_Gods_Will_%282014%29-1.mp4'>https://s04.solidfilesusercontent.com/NGU0MGIwZTg1MDU0MjI5YzE3YzM3NDQ4ZWJhNjVhN2Y3ZGVmN2EwNDoxYkFmRkY6ODZvb29qRG8tdFluMXl2TU1hR1dBN19ockt3/v8grz2RpkMkWd/As_The_Gods_Will_%282014%29-1.mp4</a>";

preg_match($regex, $url, $matches);

echo $matches[0];

thx

  • You want to match a URL or grab it? – chris85 Jun 08 '16 at 18:06
  • To start your regex is looking for `"` but your string has `'` around the hrefs. Another reason why a parser should be used and not regex. – chris85 Jun 08 '16 at 18:08
  • @chris85 i want to match and grab it. – Jennifer Laurence Jun 08 '16 at 18:08
  • `$regex` is not used in your PHP. There are a number of issues here. Looked at the linked answer. `$matches` != `$Matches`. Your delimiter is being used in the regex unescaped. Too many things here. I don't recommend using this but it would at least work. ` – chris85 Jun 08 '16 at 18:09
  • @chris85 sory, i don;t give the complete what i'm trying to do. i already update the question. – Jennifer Laurence Jun 08 '16 at 18:13
  • There are at least 6 issues with the code you've posted. Please look at the linked answer. If you are just trying to learn regexs please try your regex on regex101.com – chris85 Jun 08 '16 at 18:15
  • @chris85 how can i grab the url and echo the url chris? – Jennifer Laurence Jun 08 '16 at 18:20
  • Did you look at the linked answer? http://stackoverflow.com/questions/3820666/grabbing-the-href-attribute-of-an-a-element – chris85 Jun 08 '16 at 18:25
  • @chris85 i try your regex pattern, its work on regex101. but i get result white page. with no the url. i already update the question. please check it. – Jennifer Laurence Jun 08 '16 at 18:28
  • @chris85 please check https://regex101.com/r/gB4bB8/3. it's now not to work. it don't want to obtain the url. – Jennifer Laurence Jun 08 '16 at 18:36
  • You realllly should look at the linked answer it is all there. A blank page usually indicates an error so check your error logs. – chris85 Jun 08 '16 at 18:41
  • @chris85 the problem is now complicated. its not work. please try to look. https://regex101.com/r/gB4bB8/3. the link u give is already mix with javascript. – Jennifer Laurence Jun 08 '16 at 18:43
  • Where's the PHP usage? I'll leave this here https://eval.in/585620. – chris85 Jun 08 '16 at 18:45
  • @christ85 the answer use DOM. its javascript part. i tried your regex before its work. when i try with the full page with that regex,. the regex don't work. it grabs other part url anchor element. please check the link i give https://regex101.com/r/gB4bB8/3. it's match autogenerate not the solidfiles – Jennifer Laurence Jun 08 '16 at 18:49
  • It blindly finds a link. Use preg_match_all and you will get all links. Update the `href=` to what you are looking for. In regex101 give it the `g` modifier and you will see all links found. I've offered all advise i can. – chris85 Jun 08 '16 at 18:51
  • @christ85 ok, last christ please. how can i make it to match protocol https only? – Jennifer Laurence Jun 08 '16 at 19:05
  • `Update the href= to what you are looking for`. – chris85 Jun 08 '16 at 19:31

0 Answers0