1

Currently working with PHP Simple HTML DOM Parser and come across a very strange scenario.

I have a set of urls which i am crawling and using the following to get the info i need:

foreach($urls as $url) {
    $html = file_get_html($url);
    foreach($html->find('.product4block') as $article) {
    $item['title']     = $article->find('.product4text', 0)->plaintext;
    $item['link']    = $article->find('.product4text a', 0)->href;
    $item['price'] = $article->find('.product_price', 0)->plaintext;
    $data[] = $item;
  } 
}

I then get a result like the following, which is what i need:

Array
(
    [0] => Array
        (
            [title] => title 0
            [link] => link 0
            [price] => £26.99 
        )

    [1] => Array
        (
            [title] => title 1
            [link] => link 1
            [price] => £27.99 
        )

    [2] => Array
        (
            [title] => title 2
            [link] => link 2
            [price] => £30.99 
        )
)

I then loop through the data adding it to my database.

foreach($data as $result){  
  //insert data here//
}

I then need to remove the £ symbol from the price. Which i am simply doing a str_replace like this:

$price = str_replace('£', '', $result['price']);

For some strange reason the £ is not getting removed. Im not sure if its the dom parser causing issues, or if str_replace just isn't working for some reason.

Any reason why this wouldn't work?

1
  • Odd, did a quick check and your str_replace seems to be working for me, just as a check could you try altering the str_replace to remove another character in the price string. if that removes it then the issue is that the two £ characters aren't the same for some reason, whether that's due to some form of character encoding I'm not 100% sure. Commented Mar 2, 2016 at 10:31

3 Answers 3

1

Well, the str_replace() works, see http://ideone.com/C5O2LW

Alternatives:

  1. Use NumberFormatter::parseCurrency http://php.net/manual/de/numberformatter.parsecurrency.php

  2. $output = (float) substr($input, strpos($input, "£") + 1);

  3. $output = floatval(ltrim($input,"£"));

  4. you don't need preg_* functions for this

Sign up to request clarification or add additional context in comments.

Comments

0
foreach($urls as $url) {
    $html = file_get_html($url);
    foreach($html->find('.product4block') as $article) {
    $item['title']     = $article->find('.product4text', 0)->plaintext;
    $item['link']    = $article->find('.product4text a', 0)->href;
    $item['price'] = $article->find('.product_price', 0)->plaintext;
    $data[] = $item;
  } 
}

replace with this it will work

foreach($urls as $url) {
    $html = file_get_html($url);
    foreach($html->find('.product4block') as $article) {
    $item['title']     = $article->find('.product4text', 0)->plaintext;
    $item['link']    = $article->find('.product4text a', 0)->href;
    $item['price'] =  trim(str_replace('£', '',$article->find('.product_price', 0)->plaintext));
    $data[] = $item;
  } 
}

no need to loop again.

Comments

0

There are many way for that. First, you can read the Multibyte String Functions

After, you can, for example, use the preg_replace functions with UTF-8 option.

At last, you can also use the ASCII number of the character.

First example :

$tab['price'] = '£26.99';
$tab['price'] = preg_replace('#£#u', '', $tab['price']);

result :

 Array
(
    [price] => 26.99
)

note the "u" just after the regular expression.

2nd example :

echo ord('£'); // show 194
$price=str_replace(chr(194),'',$price);
echo $price; // show 26.99

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.