EHerman
EHerman

Reputation: 1892

Regex contents outside of two tags

I am trying to extract contents that lie outside two sets of html tags.

The HTML is set up like so:

<div class="col-md-4 col-sm-6 col-lg-3">
    <small class="text-muted pull-right">4.4</small>
    <i class="custom-icon"></i>
    desired content to retrieve
    <span class="text-muted">some other text here</span>
</div>

I need to retrieve the content "desired content to retrieve" which lies after the </i> and before the <span class="text-muted">.

I've tried:

$custom_regex= '#</i>(.*?)<span class="text-muted">#';

$text_scan = preg_match_all( $custom_regex, $content_to_scan, $text_array );

with no success. The $text_array variable returns empty.

I'm not that great with regex, so maybe my expression is incorrect for what I'm after.

Upvotes: 2

Views: 67

Answers (2)

streetturtle
streetturtle

Reputation: 5850

Wouldn't usage of lookarounds be better?

(?<=<\/i>)\s*(.*?)\n.*(?=<span)

Demo: https://regex101.com/r/zK2wD8/8

Upvotes: 1

ryuichiro
ryuichiro

Reputation: 3865

If you insist on regex, try this.

/<\/i>\s*(.*?)\n.*<span class="text-muted"/g

Upvotes: 0

Related Questions