gpt4 book ai didi

php - 字符串中的下标数字

转载 作者:可可西里 更新时间:2023-10-31 22:10:26 27 4
gpt4 key购买 nike

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

例如:

$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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com