user2289809
user2289809

Reputation: 53

Php simple html dom parser find string with any character

I have this html

<div class="price-box">

                <p class="old-price">
            <span class="price-label">This:</span>
            <span class="price" id="old-price-326">
                8,69 €                </span>
        </p>

                    <p class="special-price">
            <span class="price-label">This is:</span>
            <span class="price" id="product-price-326">
                1,99 €                </span> <span style="">/ 6.87 </span>
        </p>


    </div>

I'm need get "1,99 €", but the id 'product-price-326' is generating random numbers. How to find 'product-price-*'? I'm trying

foreach($preke->find('span[id="product-price-[0-9]"]') as $div) 

and

foreach($preke->find('span[id="product-price-"]') as $div) 

but it doesn't work.

Upvotes: 0

Views: 1225

Answers (4)

Test User
Test User

Reputation: 11

Try Like This Might Be Works

foreach($preke->find('span[id^="product-price-"]')  as $div) { /* Code */ } 

Upvotes: 1

D4V1D
D4V1D

Reputation: 5849

As per my comment, here's what you need to do:

foreach($preke->find('span[id^="product-price-"]')  as $div) {} // note the ^ before the =

^= means starts with.

Upvotes: 3

Mubin
Mubin

Reputation: 4425

why not to get it using class?

echo $preke->find('.special-price', 0)->find('.price', 0)->plaintext;

this will get you 1,99 €

Upvotes: 0

Chris Brand
Chris Brand

Reputation: 1990

I am not sure what $preke is, but if it's a DOM selector that supports proper class selectors you can use

$preke->find('span[id^="product-price"]')

or

$preke->find('span[id*="product-price"]')

The ^= tells it to look for elements that has an ID starting with "product-price" and the *= tells it to look for elements that has an ID that contains "product-price".

Upvotes: 2

Related Questions