【问题标题】:Convert simple codes to html styles将简单代码转换为 html 样式
【发布时间】:2015-01-03 11:59:55
【问题描述】:

我正在制作一个脚本,用于将某些特定标签转换为已知且有效的 html,例如

[b] bold [/b] for <span style="font-weight:bold"> bold</span>

[color=red] red text [/color] for <span style="font-color:red"> red</span>

[fs=15]big font[/fs] for <span style="font-size:15px"> big font</font>

and [link=http://www.gooole.com target=new title=goole] google[/link] to be converted to

<a href="http://www.gooole.com" title="goole">google</a>

也可以像 [fs=15] 这样混合它们,这很大。 [color=red] 红色文字[/color] [/fs]

这是我使用的代码-

$str = preg_replace( '/\[b\]/', '<span style="font-weight:bold">', $str );
$str =preg_replace( '/\[\/b\]/', '</span>', $str );
$str= preg_replace( '/\[\/fs\]/', '</span>', $str );
$str= preg_replace( '/\[fs=(.*)\]/', '<span style="font-size:$1px">', $str );

$str= preg_replace( '/\[\/color\]/', '</span>', $str );
$str= preg_replace( '/\[color=(.*)\]/', '<span style="font-color:$1">', $str );

此代码在未嵌套使用时可以正常工作,并且如果标记没有 = 属性,也可以在嵌套中使用。当我使用这样的东西时会出现问题

[fs=15] this is big. [fs=12] this is big.  [/fs] [/fs]

它给了我

<span style="font-size:15] this is big. [fs=12px"> this is big. </span> </span>

应该是这样的

<span style="font-size:15px> this is big. <span style="font-size:12px> this is big. </span> </span>

它可以正常工作

[b] hi [i] ok [/i] yes [/b]

请建议我不太了解正则表达式。

【问题讨论】:

  • 使用非贪婪匹配:(.*?)
  • @hjpotter92 请查看我编辑的问题。(您的代码运行良好,谢谢,但我需要多一步!)
  • 我用了好几次,是否可以只用一个 preg_replace 替换所有代码?

标签: php regex preg-replace preg-replace-callback


【解决方案1】:

使用非贪婪选项:

$str = preg_replace( '/\[fs=(.*)\]/U', '<span style="font-size:$1px">', $str );

并且更喜欢:

$str = preg_replace( '/\[fs=(.*)\](.*)\[\/fs\]/U', '<span style="font-size:$1px">$2</span>', $str );

【讨论】:

  • 正如消息所说,[ 是打开的而不是关闭的,所以我们将其转义:$str = preg_replace( '/\[fs=(.*)\](.*)\[\/fs\]/U', '&lt;span style="font-size:$1px"&gt;$2&lt;/span&gt;', $str );
  • 感谢您的回答
【解决方案2】:
  1. 因为你总是用&lt;/span&gt;替换结束标签;将它们包含在一个中。
  2. 您可以使用哈希映射来匹配相似的标签结构;如[b][i] 等,并使用preg_replace_callback 中的散列结构
  3. 使用不贪婪(或惰性)匹配可能忽略大小写modifier。此外,请使用 / 以外的其他分隔符。

试试下面的代码:

// first deal with closing tags
$str = preg_replace( '#\[/(color|b|i|fs|so|many|tags|can|go|here)\]#i', '</span>', $str );
// now some functions; with hashmaps
function colsize( $m ) {
    $map = [    // or $map = Array(
        'color' => 'color: %s',
        'fs' => 'size: %dpx'
    ];    // or );
    return sprintf( '<span style="font-' . $map[$m[1]] . ';">', $m[2] );
}
function emph( $m ) {
    $map = [    // or $map = Array(
        'b' => 'weight: bold',
        'i' => 'style: italic'
    ];    // or );
    return '<span style="font-' . $map[$m[1]] . ';">';
}
// using the custom functions from above now
$str = preg_replace_callback( '@\[(color|fs)=([^\]]+)\]@iU', 'colsize', $str );
$str = preg_replace_callback( '@\[([bi])\]@i', 'emph', $str );

【讨论】:

  • 好的,但是我如何传递多个属性,例如 [link=google.comtarget=new] Google [/link]
  • @RNKushwaha $str = preg_replace( '#\[/link=(\S+) target=([^\]]+)\]#i', '&lt;a href="{$1}" target="_{$2}"&gt;', $str );
猜你喜欢
  • 2011-12-19
  • 1970-01-01
  • 1970-01-01
  • 2017-07-28
  • 2021-10-09
  • 1970-01-01
  • 2013-10-24
  • 1970-01-01
  • 2022-01-10
相关资源
最近更新 更多