PHP 如何查找字符串中以 $ 或 £ 符号开头的所有价格并将其替换为 Bootstrap 标签

标签 php twitter-bootstrap replace preg-replace str-replace

我有以下文字:

You can purchase the red umbrella for just $14.99 and the blue one for £12.49. Limited time only

我想在字符串中查找 $14.99 价格,并将价格包装在 Bootstrap 标签中,对于 £12.49 也是如此。

有什么想法吗?

最佳答案

使用 PHP,您可以使用 strpos()获取每个 $ 的位置(以及键盘上没有的其他符号)...

然后,一旦你有了位置,只要确保你正在查看的是美元金额,方法是在子字符串上再次执行 strpos 以找到 $ 等之后最近的 .(点)...

<?php
$string = 'some stuff $14.99';
$pos = strpos($string, '$');
//pos = 11
$newstring = substr($string, $pos);
$dotpos = strpos($string, '.');
$yourfinalstring = substr($string, $pos, $dotpos + 2);
//your final string = $14.99
?>

在 javascript 中只需使用 indexOf()

var string = 'some stuff $14.99';
var pos = string.indexOf('$');
// pos = 11

关于PHP 如何查找字符串中以 $ 或 £ 符号开头的所有价格并将其替换为 Bootstrap 标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30076091/

相关文章:

PHP preg replace 只允许数字

jquery - 如何更改 html 文件中的 Bootstrap 数据颜色属性

html - Bootstrap 导航栏列表项在单击时更改其颜色

html - 旋转木马图像不在 bootstrap css 中旋转

php - Symfony 3 表单类型测试

php - 找不到 httpd.conf PHP 模块

php - 使用 PHP7 的 Cakephp

python - 从 fileS 的单词中创建一个列表,并用该列表对照 python 中的文件进行检查

python - 多个字符串替换为python中的位置

java - 从字符串中删除以逗号分隔的值