Ricardo
Ricardo

Reputation: 1

Get a string between 2 strings/tags in php

I'm tring to get a string between 2 strings with preg_match

The string is something like this, this is just an example

<source src='http://website.com/384238/dsjfjsd.jpg' type='image/jpg' data-res='43543' lang='English'/>

I want the link, the "data-res=" is the one that varies so:

I'm doing something like this:

preg_match("<source src='(.*)' type='image/jpg' data-res='43543",$input,$output);

I also tried this way

$output = trim(cut_str($input, '<source src='', ' type='image/jpg' data-res='43543'));

I think the problem is not knowing how do I represent the spaces or special chars, I also wanted an advice for whats the best function to solve this

Upvotes: 0

Views: 131

Answers (3)

David B&#233;langer
David B&#233;langer

Reputation: 7438

Why not parsing it like this ? It's faster then REGEX and easier to use.

$dom = new DOMDocument;
$dom->loadHTML('<source src="http://website.com/384238/dsjfjsd.jpg" type="image/jpg" data-res="43543" lang="English" />');

//  We read it
$dataSource = $dom->getElementsByTagName('source');

//  We loop on it
$dataRes = FALSE;
foreach($dataSource as $data){
    #   We read the wanted field
    if(($dataAttr = $data->attributes->getNamedItem('data-res')->nodeValue) == "43543"){
        #   We assign it
        $dataRes&= $dataAttr;

        #   Done - We end the loop here
        break;
    }
}

#   We found it ?
if($dataRes !== FALSE){
    #   Yes
    var_dump($dataRes);
} else {
    #   No
    exit('Failed');
}

Warning: I didn't not test this code but it should work.

Upvotes: 0

Cristiana
Cristiana

Reputation: 1

Here is a code you could try:

// The Regular Expression filter
$reg_exSRC = "/(src)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/";

// The text you want to filter for urls
$text = "<source src='http://website.com/384238/dsjfjsd.jpg' type='image/jpg' data-res='43543' lang='English'/>";

// apply expression to the text
preg_match($reg_exSRC, $text, $url);

echo $url[0];

Upvotes: 0

Jason McCreary
Jason McCreary

Reputation: 72991

While you can do this with a regular expression. I would encourage you to use DOMDocument.

From there it would be simple to grab all source tags using getElementByTagName():

$dom = new DOMDocument;
$dom->loadHTML($html);
$source_tags = $dom->getElementsByTagName('source');
foreach ($source_tags as $source_tag) {
    echo 'Link: ' . $source_tag->attributes->getNamedItem('src')->nodeValue;
}

This question might also help if you are interested in source tags with the data-res attribute.

Upvotes: 1

Related Questions