【问题标题】:eliminate 3 backslashes with stripslashes用 stripslashes 消除 3 个反斜杠
【发布时间】:2017-07-09 00:15:25
【问题描述】:

出于某些原因,在更新了几项内容后,在我们的 wordpress 网站上的自定义字段中会自动在每个撇号前添加 3 个反斜杠。 示例src="abc" 将导致 src=\\\"abc\\\"

我在functions.php 中有一个函数,用于连接网站。现在我需要删除那些反斜杠。这是原始函数:

add_action('woocommerce_before_single_product', 'headline_placeholder');
function headline_placeholder () {
global $wp_query;
$postid = $wp_query->post->ID;
echo get_post_meta($postid, 'productheadline', true);
wp_reset_query();
}

这是我尝试删除反斜杠的方法,但它只删除了 2 个反斜杠,而不是全部 3 个。

function removeslashes($string)
{
    $string=implode("",explode("\\",$string));
    return stripslashes(trim($string));
}

add_action('woocommerce_before_single_product', 'headline_placeholder');
function headline_placeholder () {
global $wp_query;
$postid = $wp_query->post->ID;
$meta = get_post_meta($postid, 'productheadline', true);
echo removeslashes($meta);
wp_reset_query();
}

错在哪里?

【问题讨论】:

    标签: php wordpress stripslashes


    【解决方案1】:

    也许你可以使用 str_replace,像这样:

    function removesalshes($text) {
        return str_replace ( "///" , "", $text);
    }
    

    希望对你有帮助!

    【讨论】:

    • 感谢您的想法。我将其更改如下,但它没有删除任何反斜杠。你知道为什么吗? function removeslashes($text) { return str_replace ( "///" , "", $text); } add_action('woocommerce_before_single_product', 'headline_placeholder'); function headline_placeholder () { global $wp_query; $postid = $wp_query->post->ID; $meta = get_post_meta($postid, 'productheadline', true); echo removeslashes($meta); wp_reset_query(); }
    【解决方案2】:

    您似乎正在删除正斜杠。请尝试以下操作:

    function removesalshes($text) {
        return str_replace ( '\\\' , "", $text);
    }
    

    【讨论】:

    • 是的,你是对的,这是一个问题,但出于某种原因,你不能在其中添加三个斜杠(或任何其他奇数),因为下面的代码不起作用。我还尝试了 6 个斜杠,但还剩下一个反斜杠。我也尝试禁用magic_quotes,但它不起作用。我不知道为什么..
    • 反斜杠在 php.ini 中有特殊用途。如果一个反斜杠后面跟着另一个反斜杠,则这两个反斜杠被视为一个斜杠。本文讨论了它:stackoverflow.com/a/4764746/4508593。如果您确定您的文本不包含任何有用的斜杠,您可以使用以下命令删除所有斜杠:str_replace ( '\\' , "", $text);
    • 啊感谢这个链接!基本上这个函数可以工作,但是这 3 个反斜杠之一仍然存在于每个撇号前面。
    • 您是否在代码中使用了addlashes 函数。似乎您的代码在将数据保存到数据库之前添加了斜杠
    • 否 - 正常的 wordpress 网站。而且它只在自定义字段中,我将哪些内容挂接到产品页面中。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-12-04
    • 1970-01-01
    • 1970-01-01
    • 2012-09-01
    • 2011-01-23
    • 1970-01-01
    相关资源
    最近更新 更多