Reputation: 5846
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?
Upvotes: 1
Views: 793
Reputation: 41756
Well, the str_replace() works, see http://ideone.com/C5O2LW
Alternatives:
Use NumberFormatter::parseCurrency http://php.net/manual/de/numberformatter.parsecurrency.php
$output = (float) substr($input, strpos($input, "£") + 1);
$output = floatval(ltrim($input,"£"));
you don't need preg_*
functions for this
Upvotes: 1
Reputation: 511
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
Upvotes: 0
Reputation: 1082
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.
Upvotes: 0