字符串中的下标数字
Subscript digits from a string
我想为字符串中的每个数字添加下标。
例如:
$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);
但是我怎样才能对数字应用下标效果并打印字符串呢?
这可能有帮助:
$str = '1Department of Chemistry, College of 2Education for Pure Science';
preg_match_all('!\d+!', $str, $matches);
foreach($matches[0] as $no){
$str = str_replace($no, '<sub>'.$no.'</sub>', $str);
}
echo htmlentities($str);
将给出输出:
<sub>1</sub>Department of Chemistry, College of <sub>2</sub>Education for Pure Science
或preg_replace
将给出相同的输出:
$str = '1Department of Chemistry, College of 2Education for Pure Science';
$str = preg_replace( '!\d+!', '<sub>[=12=]</sub>', $str );
echo htmlentities($str);
您可以使用 preg_replace
:
preg_replace( '!\d+!', '<sub>[=10=]</sub>', $str );
<?php
function subscript($string)
{
return preg_replace('/(\d+)/', '<sub>\1</sub>', $string);
}
$input = '1Department of Chemistry, College of 2Education for Pure Science';
$expected = '<sub>1</sub>Department of Chemistry, College of <sub>2</sub>Education for Pure Science';
$output = subscript($input);
if ($output === $expected) {
printf('It works! %s', htmlentities($output));
} else {
printf('It does not work! %s', htmlentities($output));
}
我假设你想要这样的东西
$string = '1Department of Chemistry, College of 2Education for Pure Science';
$pattern = '/(\d+)/';
$replacement = '<sub></sub>';
echo preg_replace($pattern, $replacement, $string);
找到的号码将替换为子标签中的号码本身。该示例取自 PHP preg-replace 手册,您可以在此处找到 http://php.net/manual/en/function.preg-replace.php
我已经知道你已经接受了答案,但我仍然发布这个答案是因为我在处理它,其次这可能对将来的其他人有帮助。
<?php
$str = '1Department of Chemistry, College of 2Education for Pure Science';
$strlen = strlen( $str );
$numbers = array();
$replace = array();
for( $i = 0; $i <= $strlen; $i++ ) {
$char = substr( $str, $i, 1 );
// $char contains the current character, so do your processing here
if(is_numeric($char)){
$numbers[] = $char;
$replace[] = "<sub>".$char."</sub>";
}
}
echo $str = str_replace($numbers, $replace, $str);
?>
我想为字符串中的每个数字添加下标。
例如:
$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);
但是我怎样才能对数字应用下标效果并打印字符串呢?
这可能有帮助:
$str = '1Department of Chemistry, College of 2Education for Pure Science';
preg_match_all('!\d+!', $str, $matches);
foreach($matches[0] as $no){
$str = str_replace($no, '<sub>'.$no.'</sub>', $str);
}
echo htmlentities($str);
将给出输出:
<sub>1</sub>Department of Chemistry, College of <sub>2</sub>Education for Pure Science
或preg_replace
将给出相同的输出:
$str = '1Department of Chemistry, College of 2Education for Pure Science';
$str = preg_replace( '!\d+!', '<sub>[=12=]</sub>', $str );
echo htmlentities($str);
您可以使用 preg_replace
:
preg_replace( '!\d+!', '<sub>[=10=]</sub>', $str );
<?php
function subscript($string)
{
return preg_replace('/(\d+)/', '<sub>\1</sub>', $string);
}
$input = '1Department of Chemistry, College of 2Education for Pure Science';
$expected = '<sub>1</sub>Department of Chemistry, College of <sub>2</sub>Education for Pure Science';
$output = subscript($input);
if ($output === $expected) {
printf('It works! %s', htmlentities($output));
} else {
printf('It does not work! %s', htmlentities($output));
}
我假设你想要这样的东西
$string = '1Department of Chemistry, College of 2Education for Pure Science';
$pattern = '/(\d+)/';
$replacement = '<sub></sub>';
echo preg_replace($pattern, $replacement, $string);
找到的号码将替换为子标签中的号码本身。该示例取自 PHP preg-replace 手册,您可以在此处找到 http://php.net/manual/en/function.preg-replace.php
我已经知道你已经接受了答案,但我仍然发布这个答案是因为我在处理它,其次这可能对将来的其他人有帮助。
<?php
$str = '1Department of Chemistry, College of 2Education for Pure Science';
$strlen = strlen( $str );
$numbers = array();
$replace = array();
for( $i = 0; $i <= $strlen; $i++ ) {
$char = substr( $str, $i, 1 );
// $char contains the current character, so do your processing here
if(is_numeric($char)){
$numbers[] = $char;
$replace[] = "<sub>".$char."</sub>";
}
}
echo $str = str_replace($numbers, $replace, $str);
?>