php - 字符串中的下标数字

标签 php regex subscript

我想下标字符串中的每个数字。

例如:

$str = '1Department of Chemistry, College of 2Education for Pure Science';

我想要的输出:

<sub>1</sub>Department of Chemistry, College of <sub>2<sub>Education for Pure Science

我从一个字符串中获取了所有数字:

//digits from string 
preg_match_all('!\d+!', $str, $matches);
print_r($matches);

但是我怎样才能对数字应用下标效果并打印字符串呢?

最佳答案

您可以使用 preg_replace :

preg_replace( '!\d+!', '<sub>$0</sub>', $str );

Demo

关于php - 字符串中的下标数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32969564/

相关文章:

PHP 和 MySQL 工作台

php - finfo 返回的类型错误

PHP 垃圾收集器不断删除我的 session

javascript - 电子邮件地址验证的最佳做法(包括 gmail 地址中的 +)

Python - 在正则表达式中使用变量

ios - UITextView 中的子/上标

php - 如何使用 Goutte 在 laravel 5.2 中进行抓取?

python - 负向后看与或条件?

json - swift ,错误: ambiguous use of subscript

python - 在 python 中打印下标