Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 2nd, 2012  |  syntax: None  |  size: 1.13 KB  |  hits: 10  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Get the number of matched characters in a regex group
  2. preg_replace('/(?:n|^)(={3,6})([^=]+)(1)/','<h#>$2</h#>', $input);
  3.        
  4. ===Heading 3=== into <h3>Heading 3</h3>
  5. ====Heading 4==== into <h4>Heading 4</h4>
  6. ...
  7.        
  8. preg_replace('/(?:n|^)(={3,6})([^=]+)(1)/e',
  9.              "'<h'.strlen('$1').'>'.'$2'.'</h'.strlen('$1').'>'", $input);
  10.        
  11. preg_replace_callback('/(?:n|^)(={3,6})([^=]+)(1)/', 'cb_headline', $input);
  12.  
  13.   function cb_headline($m) {
  14.       list(, $markup, $text) = $m;
  15.  
  16.       $n = strlen($markup);
  17.       return "<h$n>$text</h$n>";
  18.   }
  19.        
  20. $str = '===Heading 3===';
  21. echo preg_replace('/(?:n|^)(={3,6})([^=]+)(1)/e',
  22.      'implode("", array("<h", strlen("$1"), ">$2</h", strlen("$1"), ">"));',
  23. $str);
  24.        
  25. echo preg_replace('/(?:n|^)(={3,6})([^=]+)(1)/e',
  26.      '"<h".strlen("$1").">$2</h".strlen("$1").">"',
  27. $str);
  28.        
  29. <?php
  30. $input = '===Heading 3===';
  31. $h_tag = preg_replace_callback('#(?:n|^)(={3,6})([^=]+)(1)#', 'paragraph_replace', $input);
  32. var_dump($h_tag);
  33.  
  34. function paragraph_replace($matches) {
  35.     $length = strlen($matches[1]);
  36.     return "<h{$length}>". $matches[2] . "</h{$length}>";
  37. }
  38. ?>
  39.        
  40. string(18) "<h3>Heading 3</h3>"