user311509
user311509

Reputation: 2866

Fetch Single Number using Preg_Match

    <span class="nowrap">
        <span class="use_sprites stars4 stars4i5" title="5 start hotel">&nbsp;
    </span>

I need to fetch the single number 5 that is in between title.

Here is my attempt:

preg_match_all('#<span class="nowrap">\s*(.*?)\s*<\/span>#s', $htmlOfPage, $rate);

The above code fetches the following:

<span class="use_sprites stars4 stars4i5" title="5 start hotel">&nbsp;

Question:

I need to start from Starting point and ignore everything except title="..." and in title grab the single number. By now, we already at the end point.

Any help how to do the above steps?

Upvotes: 1

Views: 109

Answers (1)

James L.
James L.

Reputation: 4097

$str = '<span class="nowrap">
        <span class="use_sprites stars4 stars4i5" title="5 start hotel">&nbsp;
    </span>';

preg_match('/<span class="nowrap">.+? title="([0-9]){1}/sm', $str, $match);

print_r($match);

Output:

Array
(
    [0] => <span class="nowrap">
        <span class="use_sprites stars4 stars4i5" title="5
    [1] => 5
)

Make sure you look at the source code for the match. It will not show up in the browser window because of the span tags

That should just match the number you are looking for

Upvotes: 2

Related Questions