$str = '5 stars, 05 stars, 05. stars, 05stars';
$str = preg_replace('/0?5.? stars/', 'stars', $str); // replace 5 stars, 05 stars, 05. stars to stars
$str = str_replace('05stars', '5stars', $str); // 05stars replace to 5stars
echo $str; // stars, stars, stars, 5stars
but. number in the middle of text also gone.Peter said:Will this do?
Code:$str = preg_replace('/\d+.?\d*\s+([^\s]+)/', '$1', $str);
$str = preg_replace('/^\d+.?\d*\s+([^\s]+)/', '$1', $str);
$str = ltrim($str, '0'); //remove leading 0 if not replaced by preg_replace
$str = "05 spac3 9test 9 star 0.5 something";
$tmp_arr = explode(' ',$str);
$new_arr = array_filter($tmp_arr, 'remove_numbers');
$new_str = implode(' ',$new_arr);
function remove_numbers($var){
return !is_numeric($var);
}
paschim said:Regex is definitely the best approach, but for simplicity, you can use array filters too.
PHP:$str = "05 spac3 9test 9 star 0.5 something"; $tmp_arr = explode(' ',$str); $new_arr = array_filter($tmp_arr, 'remove_numbers'); $new_str = implode(' ',$new_arr); function remove_numbers($var){ return !is_numeric($var); }