 |
|
ได้แล้วครับ แหะๆ 
Code (PHP)
class Modifier
{
/**
* automatically add line numbers class to pre and code content.
*
* @link http://www.devnetwork.net/viewtopic.php?f=38&t=102670 long pattern from here.
* @param string $content
* @return string
*/
public function automaticAddLineNumbersClass($content = '')
{
$pattern = '/<pre?(([^>]*)class=["|\'](?<class>[^"]*)["|\'])?.*\s*><code/iu';
$content = preg_replace_callback($pattern, [&$this, 'replaceAddClassToPre'], $content);
return $content;
}// automaticAddLineNumbersClass
/**
* replace or add class to pre tag.
*
* @param array $matches
* @return string
*/
private function replaceAddClassToPre($matches)
{
if (is_array($matches) && array_key_exists(0, $matches)) {
if (strpos($matches[0], 'class=') !== false) {
$matches[0] = str_replace('\'', '"', $matches[0]);
return str_replace('class="', 'class="line-numbers ', $matches[0]);
} elseif (strpos($matches[0], '<pre>') !== false) {
return str_replace('<pre', '<pre class="line-numbers"', $matches[0]);
} else {
return $matches[0];
}
}
}// replaceAddClassToPre
}
|
 |
 |
 |
 |
Date :
2016-03-24 18:37:21 |
By :
mr.v |
|
 |
 |
 |
 |
|
|
 |