/    Sign up×
Community /Pin to ProfileBookmark

preg_match help!

<meta name=”title” content=”RANDOM TEXT”/><link rel=”image_src” href=”RANDOMURL”/>

I need help being able to find the random image url in this string, there will be many other occurrences of the same image url on the page, and other urls but i need to find this specific one. Any help is much appreciated!

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@eval_BadCode_Feb 13.2011 — hmm... Filter the string first, FILTER_VALIDATE_URL

http://us.php.net/parse_url

PHP_URL_PATH ?

$types = array("gif", "GIF", "jpg", "JPG", "ico", "ICO", "png", "etc");

iterate, and check in_array(end(explode(".",$iter)),$types));


If someone wants to figure out the regex way:

urls (including relative images) can only have a-z A-Z 0-9 - . _ ~ : / ? # [ ] @ ! $ & ' ( ) * + , ; =
Copy linkTweet thisAlerts:
@NogDogFeb 13.2011 — Probably more robust than a regexp approach:
[code=php]
<?php
$testData = <<<EOD
<html><head>
<title>Title</title>
<meta name="title" content="RANDOM TEXT"/><link rel="image_src" href="RANDOMURL"/>
</head><body>
<p>This is a test</p>
</body></html>
EOD;

$url = false;
$dom = new DOMDocument();
$dom->loadHTML($testData);
$links = $dom->getElementsByTagName('link');
foreach($links as $link) {
if($link->hasAttribute('rel') && $link->getAttribute('rel') == 'image_src') {
if($link->hasAttribute('href')) {
$url = $link->getAttribute('href');
break;
}
}
}
if($url) {
echo "URL: " . $url;
}
else {
echo "Not found.";
}
[/code]
×

Success!

Help @robertbarzyk spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...