-1

Can't find anything that works. Need to remove everything before the first and after the second doulble quotes:

Example:

<a href="http://somesite.com"><img src="http://somesite.com/image.png"></a>

The result should be:

http://somesite.com

Thank you very much!

SOLVED: see @ankabout 's solution.

MrK
  • 21
  • 3
  • And.. why `preg_replace`? Doesn't `str_replace` suffice? – Xorifelse Apr 02 '18 at 17:36
  • `/http:\/\/(?:www\.)?([a-z0-9\-]+)(?:\.[a-z\.]+[\/]?)[^ ]*/i` – Xorifelse Apr 02 '18 at 17:38
  • `preg_match` and extracting would be simpler than replacing. But if you haven't tried / found anything (sounds unlikely) then go for a HTML processor (seeing that's what you have). That's simpler to maintain than a regex that you don't understand. – mario Apr 02 '18 at 17:38

2 Answers2

1
$html = '<a href="http://somesite.com"><img src="http://somesite.com/image.png"></a>';
$dom = new DOMDocument();
$dom->loadHTML($html);
var_dump($dom->getElementsByTagName('a')[0]->getAttribute('href'));
Ermac
  • 1,181
  • 1
  • 8
  • 12
  • After playing with your solution I realized that it is a better one. Works in ALL instances. – MrK Apr 03 '18 at 00:35
  • Hello, sorry I cheated a little I just copied this solution https://stackoverflow.com/a/3820783/7841955 (I wanted to gain some points :) ), though apparently this is the best solution to parse HTML. But you could use some string manipulation functions (for better performance) if you're really sure of the HTML format, another way to grab the link would be `var_dump(strstr(substr(strstr($html, '"'), 1), '"', true))` – Ermac Apr 03 '18 at 11:12
-1

I thing what you need is someting like this.

<?php
    $input = '<a href="http://somesite.com"><img src="http://somesite.com/image.png"></a>';
    $parse = '<a href=\x22(.+?)\x22>' ;
    preg_match_all($parse, $input, $out, PREG_PATTERN_ORDER);
    echo $out[1][0];