There is a list urls like
"http://books.rediff.com/categories/travel/general/2288"

I wish to find out whether they start from "http:/" or are relative urls starting from "/", if true store them in an array !

...
if (strpos($html->href, 'http://') <= 0) {
        array_push($urlarray, $link->href);
}
...

This does not work ....
How should I do it ??

Please help!

Recommended Answers

Use parse_url()

Jump to Post

str_pos() returns boolean (TRUE or FALSE)

Incorrect. It returns the index (starting from 0) at which the needle was found in the haystack.

If the needle wasn't found, it returns false. Hence why you need to test for absolutely equal to false, rather than equal to false to …

Jump to Post

All 5 Replies

Member Avatar

Use parse_url()

str_pos() returns boolean (TRUE or FALSE). Should be

if (strpos($html->href, 'http://') !== FALSE) {
    //there is http in the url, then do this statement
    // your code
}

Or use @diafol method.

str_pos() returns boolean (TRUE or FALSE)

Incorrect. It returns the index (starting from 0) at which the needle was found in the haystack.

If the needle wasn't found, it returns false. Hence why you need to test for absolutely equal to false, rather than equal to false to avoid false positives if 0 is returned.

commented: Thanks for correcting. +8

Oop ! Sound likes suck! But, you're right. :) Thanks for alert @blocblue.

if (strpos($link->href, 'http://') === 0) {
        array_push($urlarray, $link->href);
    }

I have corrected that ... "===" was to be used rather than "==" or "<=" etc.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.