Yannis Assael
Yannis Assael

Reputation: 1119

PHP Regular Expression HTML

I have a $source var which I get with curl and contains the following commented out string

//"url":"http://lh5.ggpht.com/_EpgGIto9934/TKXKqAw7uFI/AAAAAAAAGrM/PrQiCNyUdEo/8827.jpg","
$regex = "!url/"/:/"(.*)8827/.jpg!U";
preg_match_all($regex, $source, $res);
var_dump($res);

I want to get the http://.....jpg address What am I doing wrong? Thanks

Upvotes: 0

Views: 161

Answers (3)

dlock
dlock

Reputation: 9577

You may use that regex

http.+\....(?=",".+)

That will work for all 3 letters extensions.

Upvotes: 0

Brian Clapper
Brian Clapper

Reputation: 26211

<?php
$source = '"url":"http://lh5.ggpht.com/_EpgGIto9934/TKXKqAw7uFI/AAAAAAAAGrM/PrQiCNyUdEo/8827.jpg","';
$regex = '/^.*\/([^\/]+\.jpg).*$/';
preg_match($regex, $source, $res);
print_r($res);
$jpg = $res[1];
?>

Upvotes: 1

daniels
daniels

Reputation: 19203

That looks like json. If that's the case you won't need regular expressions for that. You can just use json_decode

<?php

$s = "//\"url\":\"http://lh5.ggpht.com/_EpgGIto9934/TKXKqAw7uFI/AAAAAAAAGrM/PrQiCNyUdEo/8827.jpg\",\"";

$regex = '/http(.+)\.jpg/';
preg_match($regex, $s, $matches);
echo $matches[0];

?>

Upvotes: 4

Related Questions