Find only certain URLs from page ... regex (semi-complete script)
Posted on
16th Feb 2014 07:03 pm by
admin
Hi guys,
What I need to do is take a page & extract all the URLs from the page & place them in an array.
However I only need to grab certain URLS
eg.
site1.com
site1.com/folder/thisfile.zip
site2.com
site2.com/some/folder/or/subfolder/1.mp3
site3.com
but then leave out of the array
site4.com
site5.com/the/script/needs/to/be/able/to/grab/sub/folders/and/files/2.mp3
Here's the script I've got so far but this will grab ALL the links ... so I need to modify this & perhaps use an if or switch statement to check whether it's a link I actually want...
<?php
$string = '<a href="http://www.example.com">Example.com</a> has many links with
examples <a href="http://www.example.net/file.php">links</a> to many sites and
even urls without links like http://www.example.org just to fill the gaps and
not to forget this one http://phpro.org/tutorials/Introduction-to-PHP-Regex.html
which has a space after it. The script has been modifiied from its original so now
it grabs ssl such as https://www.example.com/file.php also';
/**
*
* @get URLs from string (string maybe a url)
*
* @param string $string
*
* @return array
*
*/
function getUrls($string)
{
$regex = '/https?://[^" ]+/i';
preg_match_all($regex, $string, $matches);
return ($matches[0]);
}
$urls = getUrls($string);
foreach($urls as $url)
{
echo $url.'<br />';
}
?>
No comments posted yet
Your Answer:
Login to answer
339
23
Other forums
Coding question?
Hey guys, I have a quick question. If I want to make a way for people to pay for health in my game,
PHP, Arrays & A Lot Of Confusion
I've been doing basic PHP work on sites for a few years now, but I've never been tempted to learn ho
elseif help!
OK so I'm new to the site, as well as PHP altogether (Trying my hand at managing an online RPG) and
Which PHP-Editor do you think is the best?
Hi everyone,
Myself Brian Anderson. For the last 2 years i have been working a
Displaying Multidimensional Arrays...about to put my head in a vice, PLEASE HELP
I am fairly new to PHP and MySQL and I have a problem which I have been trying to resolve for days.
Odd or Even
Inside of a while loop I need to echo several rows of info...easy enough.
In the loop (for th
Converting from one format to another
"I have got a date in DD/MM/YYYY but I need it in MM-DD-YYYY. Help!"
How do i use vars in an array
Hiya peeps!
How do I use a var in an array. You will see $id but it isnt working.
Security Exception on pages using AJAX
I am getting the exception: attempted to perform an operation not allowed by the security policy on
update the selected existing records of database
Hi frndz,
I am new to php and get stuck....
Edit.php > Update.php > Updatea