0

I have a web page source code that I want to use in my project. I want to use an image link in this code. So, I want to reach this link using regex in PHP.

That's it:

img src="http://imagelinkhere.com" class="image"

There is only one line like this. My logic is to get the string between

="

and

" class="image"

characters.

How can I do that with REGEX? Thank you very much.

1

6 Answers 6

3

Don't use Regex for HTML .. try DomDocument

$html = '<html><img src="http://imagelinkhere.com" class="image" /></html>';

$dom = new DOMDocument();
$dom->loadHTML($html);
$img = $dom->getElementsByTagName("img");

foreach ( $img as $v ) {
    if ($v->getAttribute("class") == "image")
        print($v->getAttribute("src"));
}

Output

http://imagelinkhere.com
Sign up to request clarification or add additional context in comments.

5 Comments

The problem is, there is 30 or 40 images in this code. I want to use one of them, not all of them. They don't have " class="image" at the end. Only one has it, and I want to use that one. That's why I said between =" and " class="image" characters, and wanted to use regex.
Can you add the fill HTML to pastbin.com .. so that i can have an idea what you want .. i still belive it can be done with DomDocument
Sure, that's code. pastebin.com/qBN7K7xe And that's the link I want to get: i.milliyet.com.tr/YeniAnaResim/2012/12/12/…
just add if ($v->getAttribute("class") == "image") to the code
Thanks a lot, that's just simpler than the headache regex.
1
preg_match("/(http://+.*?")/",$text,$matches);
var_dump($matches);

The link would be in $matches.

Comments

1

Using

.*="(.*)?" .*

with preg replace gives you only the url in the first regex group (\1).

So complete it would look like

$str='img src="http://imagelinkhere.com" class="image"';
$str=preg_replace('.*="(.*)?" .*','$1',$str);
echo $str;

-->

http://imagelinkhere.com

Edit: Or just follow Baba's advice and use DOM Parser. I'll remember that regex will give you headaches when parsing html with it.

Comments

0

There is several ways to do so :

1.you can use SimpleHTML Dom Parser which I prefer with simple HTML

2.you can also use preg_match

$foo = '<img class="foo bar test" title="test image" src="http://example.com/img/image.jpg" alt="test image" class="image" />';
$array = array();
preg_match( '/src="([^"]*)"/i', $foo, $array ) ;

see this thread

Comments

0

I can hear the sound of hooves, so I have gone with DOM parsing instead of regex.

$dom = new DOMDocument();
$dom->loadHTMLFile('path/to/your/file.html');
foreach ($dom->getElementsByTagName('img') as $img)
{
    if ($img->hasAttribute('class') && $img->getAttribute('class') == 'image')
    {
        echo $img->getAttribute('src');
    }
}

This will echo only the src attribute of an img tag with a class="image"

Comments

-1

Try using preg_match_all, like this:

preg_match_all('/img src="([^"]*)"/', $source, $images);

That should put all the URL's of the images in the $images variable. What the regex does is find all img src bits in the code and matches the bit between the quotes.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.