0

I'm beginner to php and I have no Idea how to solve this.

I have text file that contains paragraphs and links, this is an example from text file:

$string = ("Lorem 'Ipsum' is simply: dummy 'http://www.exmplelink1.com/blah/blah/file 1 b.txt' text of the printing 'http://www.exmplelink1.com/blah/blah/file 1 c.txt' and typesetting industry. Lorem Ipsum 'http://www.exmplelink2.com/blah/file.txt' has been, the 'industry's standard'");

please notice that there are many single quotes and two domain names in the string: exmplelink1 and exmplelink2

  • how do I only get exmplelink1 links (the complete link between single quotes) there are two links in this case:

    'http://www.exmplelink1.com/blah/blah/file 1 b.txt'
    'http://www.exmplelink1.com/blah/blah/file 1 c.txt'
    

thank you for your help :)

poxoson
  • 5
  • 2
  • 1
    possible duplicate of [find all urls (links) in text with php](http://stackoverflow.com/questions/6065362/find-all-urls-links-in-text-with-php) – Saty May 23 '15 at 13:30
  • Use regular expressions. Something like `'http.*?'` should work. – fzzfzzfzz May 23 '15 at 13:32
  • I might add, I really don't know regex :( – poxoson May 23 '15 at 13:32
  • should I replace '?' with the domain name? because I have too many links with different domain names and I only want to get links with specific domain – poxoson May 23 '15 at 13:35

1 Answers1

1

This will work for you to get only exmplelink1 ,use file_get_contents to read you file data as string & then use below code to get you desired exmplelink1

$re = "/'http:\\/\\/www\\.exmplelink1.*?'/m"; 
$str = ("Lorem 'Ipsum' is simply: dummy 'http://www.exmplelink1.com/blah/blah/file 1 b.txt' text of the printing 'http://www.exmplelink1.com/blah/blah/file 1 c.txt' and typesetting industry. Lorem Ipsum 'http://www.exmplelink2.com/blah/file.txt' has been, the 'industry's standard'"); 

preg_match_all($re, $str, $matches);
echo '<pre>';
print_r($matches);
echo '</pre>';

See For Regex https://regex101.com/r/gE5uX6/2

A l w a y s S u n n y
  • 36,497
  • 8
  • 60
  • 103
  • May i suggest escaping the dots in hostname in the pattern? Not that it would matter this time but generally... – Cthulhu May 23 '15 at 14:23