Php smarty strip_tag允许特定标签

时间:2012-08-07 06:22:34

标签: php smarty

有没有办法在默认情况下使用php smarty中的strip_tags来允许特定的html标签传递?

stip_tags($a.message,'<p><div>');

smarty中的等价物是什么?

{$a.message|strip_tags}

3 个答案:

答案 0 :(得分:4)

我正在使用Smarty 2.6.26和strip_tags使用一个参数来指定要保留哪些标记。

试试这个:

<强> PHP:

$string = "<b>not bold</b><iframe>iframe goes away</frame> <p>paragraphed</p>
                        <div style='color:green'>div kept.</div>";
$smarty->assign('string', $string);

在模板中:

{$string|strip_tags:"<p><div>"}

<p><div>不会被剥夺。

答案 1 :(得分:2)

根据documentation,无法指定要保留的标签。但是,您可以轻松地为自己创建一个插件: http://www.smarty.net/docs/en/plugins.writing.tpl

答案 2 :(得分:2)

I know I'm a bit late, but for those who needs to do this and don't want to write a Smarty-plugin:

In Smarty you can "borrow" methods from PHP, like this.

Instead of:

public partial class MainWindow : Window
{
    //*** Method 1 & 2
    PerformanceCounter cpuCounterPi = new PerformanceCounter("Processor Information", "% Processor Time", "_Total");
    PerformanceCounter cpuCounterP = new PerformanceCounter("Processor", "% Processor Time", "_Total");

    //*** method 3
    ManagementObjectSearcher query1 = new ManagementObjectSearcher("select loadpercentage from win32_processor");

    //*** Mixed method usage below
    CounterSample csPi1, csPi2, csP1, csP2;
    double cpuPercentagePi, cpuPercentageP, cpuPercentageLoad;
    int count = -1;
    Boolean alternate = false;

    System.Windows.Threading.DispatcherTimer dispatcherTimer = new System.Windows.Threading.DispatcherTimer();

    public MainWindow()
    {
        InitializeComponent();

        dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
        dispatcherTimer.Interval = new TimeSpan(0, 0, 5);
        dispatcherTimer.Start();
    }

    private void dispatcherTimer_Tick(object sender, EventArgs e)
    {
        count++;
        //***Method 1 & 2
        if (alternate)
        {
            csPi1 = cpuCounterPi.NextSample();
            cpuPercentagePi = CounterSample.Calculate(csPi2, csPi1);
            csP1 = cpuCounterP.NextSample();
            cpuPercentageP = CounterSample.Calculate(csP2, csP1);

        }
        else
        {
            csPi2 = cpuCounterPi.NextSample();
            cpuPercentagePi = CounterSample.Calculate(csPi1, csPi2);
            csP2 = cpuCounterP.NextSample();
            cpuPercentageP = CounterSample.Calculate(csP1, csP2);
        }
        alternate = !alternate;

        if (count==5) { textBox.Clear(); count = 0; }
        textBox.Text = textBox.Text + "\nProcessor Information (Method 1)         " + cpuPercentagePi.ToString();
        textBox.Text = textBox.Text + "\nProcessor  (Method 2)                          " + cpuPercentageP.ToString();
        textBox.Text = textBox.Text + "\nProcessor ½  (Method 2 divided by 2)   " + (cpuPercentageP/2).ToString();
        //*****  Method 3 ****
        ManagementObjectCollection cpuColl = query1.Get();
        foreach (ManagementObject mo in cpuColl)
        {
            cpuPercentageLoad = Convert.ToDouble(mo["loadpercentage"]);
        }
        textBox.Text = textBox.Text +  "\nProcessor Load  (Method 3)                " + cpuPercentageLoad.ToString() + "\n";

    }

use

{$a.message|strip_tags}

Note: You only need the nofilter argument if you've set {strip_tags($a.message,"<br><div>") nofilter}