Jump to content

IS IT POSSIBLE FOR ME TO GET...


andre3

Recommended Posts

the simplest way would be with regex.

 

There are more complicated patterns that can do this more efficiently, but you'll have to specify if you want it to be more loose (about whitespace, different types of quotes, ignoring \', etc). This pattern is just for showing you the concept.

 

<?php
$string = "<a href='http://link.com'>test</a> <a href='stuff.com'>somethingelse</a>";
$result = preg_match_all("%<a href='([^']*)'>%", $string, $matches);
print_r($matches);
?>

 

the result of that would be

 

Array
(
    [0] => Array
        (
            [0] => <a href='http://link.com'>
            [1] => <a href='stuff.com'>
        )

    [1] => Array
        (
            [0] => http://link.com
            [1] => stuff.com
        )

)

 

as you can see, you can get the link you want from $matches[1][0].

Well the reason I said it is just a concept pattern, is it will only match things pretty strictly...

 

%<a href=\"([^"]*)\">%

 

will only match links of the form

 

<a href="something"> any little change such as an extra space (<a  href=...) or a different type of quote (<a href='something'>) and it won't see it. You could change it to be less strict. eg...

 

<?php
$string = '<a class="stuff" href="http://link.com" id="candyapple">test</a> <a href=\'test\'>';
$result = preg_match_all('%<a[^>]*[\s]href=[\'"]([^\'"]*)[\'"][^>]*>%i', $string, $matches);
print_r($matches);
?>

 

and that will fix some of the the problems I mentioned above... it will match " or ' for quotes, ignore extra things in the a tag, etc..

 

But it still has some limitations...

it still won't match those old htmlers who don't use quotes around their links (href=page.com)

it still won't match someone who escapes a quote in their link (href='john\'s link')

etc...

 

it really comes down to how complicated you want your pattern to be.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.