4

I want to subscript every digits from string.

For example :

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

Output I want:

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

I fetched all digits from a string :

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

But how can i apply subscript effect to digits and print string ?

Nirali Joshi
  • 1,968
  • 6
  • 30
  • 50

5 Answers5

5

You can use preg_replace:

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

Demo

BenM
  • 52,573
  • 26
  • 113
  • 168
  • Thanks ! Can you please elaborate answer? – Nirali Joshi Oct 06 '15 at 12:28
  • Elaborate how? If you click on the link to the function name, it will explain usage from the official manual. – BenM Oct 06 '15 at 12:41
  • i wanted to know what is '$0' in replacement parameter ? – Nirali Joshi Oct 06 '15 at 12:45
  • Please see the documentation: `replacement` may contain references of the form \\n or (since PHP 4.0.4) $n, with the latter form being the preferred one. Every such reference will be replaced by the text captured by the n'th parenthesized pattern. n can be from 0 to 99, and \\0 or $0 refers to the text matched by the whole pattern. Opening parentheses are counted from left to right (starting from 1) to obtain the number of the capturing subpattern. To use backslash in replacement, it must be doubled ("\\\\" PHP string). – BenM Oct 06 '15 at 12:54
1

This may help:

$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);

Will give output:

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

Or preg_replace will give same output:

$str = '1Department of Chemistry, College of 2Education for Pure Science';
$str = preg_replace( '!\d+!', '<sub>$0</sub>', $str );
echo htmlentities($str);
Manwal
  • 23,450
  • 12
  • 63
  • 93
1

I assume you want something like this

$string = '1Department of Chemistry, College of 2Education for Pure Science';
$pattern = '/(\d+)/';
$replacement = '<sub>${1}</sub>';
echo preg_replace($pattern, $replacement, $string);

The found number will be replaced with the itself within the sub tag. The example was taken from the PHP manual of preg-replace which you can find here http://php.net/manual/en/function.preg-replace.php

rpirsc13
  • 443
  • 5
  • 14
0
<?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));
}
Mike Doe
  • 16,349
  • 11
  • 65
  • 88
0

I already know you have accepted answer but still i am posting this answer because i worked on it and second may be this can be helpful for someone else in future.

<?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);

?>
Sunil Pachlangia
  • 2,033
  • 2
  • 15
  • 25