$ details ['wasprice']的值包含单词“was”(即$ 23.99)。我想删除单词“was”并将值保留为“$ 23.99”。
以下代码:
foreach($html->find('div.product-details-contents') as $content) {
$detail['img'] = $content->find('img.product-details-image',0)->src;
$detail['title'] = $content->find('span.title', 0)->plaintext;
$detail['units'] = $content->find('span.unit-size', 0)->plaintext;
$detail['wasprice'] = $content->find('span.was-price', 0)->plaintext;
$detail['nowprice'] = $content->find('span.special-price', 0)->plaintext;
$product[] = $detail;
}
print_r($product);
我只是想知道如何解决这个问题。
谢谢
答案 0 :(得分:0)
使用str_replace()
:
$detail['wasprice'] = str_replace('was ', '', $detail['wasprice']);
答案 1 :(得分:0)
if (substr($detail['wasprice'], 0, 4) === 'was ') {
$detail['wasprice'] = substr($detail['wasprice'], 4);
} else {
// ruh-roh
}
答案 2 :(得分:0)
我会在$
使用preg_replace()
:
$detail['wasprice'] = preg_replace('/([^$]*)(.*)/', '$2', $detail['wasprice']);
答案 3 :(得分:0)
第三种解决方案:
$detail['wasprice'] = ltrim($detail['wasprice'], " asw");
答案 4 :(得分:0)
使用PHP sanitize filter删除所有非数字的内容,然后输入美元($)符号。
$newprice = "$".filter_var($wasprice, FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION | FILTER_FLAG_ALLOW_THOUSAND);
在示例代码中使用它:
<?php
foreach($html->find('div.product-details-contents') as $content) {
$detail['img'] = $content->find('img.product-details-image',0)->src;
$detail['title'] = $content->find('span.title', 0)->plaintext;
$detail['units'] = $content->find('span.unit-size', 0)->plaintext;
$detail['wasprice'] = "$".filter_var($content->find('span.was-price', 0)->plaintext, FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION | FILTER_FLAG_ALLOW_THOUSAND);
$detail['nowprice'] = $content->find('span.special-price', 0)->plaintext;
$product[] = $detail;
}
print_r($product);
?>