首页 > 解决方案 > 在双引号php之前添加斜杠

问题描述

$string = '<a href="https://google.com">Link</a>';
echo addcslashes($string, '"');

但输出是

<a href="\"https://google.com\"">Link</a>

相反应该是这样的

<a href=\"https://google.com\">Link</a>

或者如果这不是正确的解决方案,那么可以用单引号替换双引号

这就是我使用它的方式,这是我目前正在做的一个模式,双引号把它搞砸了

global $post;
$guide_style = get_field('guide_style');

if ($guide_style == 'Detailed') {
    $content_rows = 'flexible_content';
} else {
    $content_rows = 'flexible_content_general';
}

$ho = get_field_object($content_rows);

if ($ho) {
    foreach ($ho['value'] as $value) {
        if ($value['questions_&_answer']) {
        $hillo = count($value['questions_&_answer']);
        //var_dump($value['questions_&_answer']);
        }
    }
}

if( have_rows($content_rows) ):
    while(the_flexible_field($content_rows)):
        if( have_rows('questions_&_answer') ): ?>
        <script type="application/ld+json">
        {
            "@context":"https://schema.org",
            "@type":"FAQPage",
            "mainEntity":[
        <?php $faqs = get_sub_field('questions_&_answer');
        $rowCount = $hillo;
        $comma = ',';
        $i = 1;
        while ( have_rows('questions_&_answer') ) : the_row();?>
        {"@type":"Question","name":"<?=get_sub_field('question', false, false)?>","acceptedAnswer":{"@type":"Answer","text":"<?=addcslashes(get_sub_field('answer', false, false), '"')?>"}}<?=($i < $rowCount) ? $comma : '';?><?php $i++; endwhile; ?>
            ]
        }
        </script>
        <?php 
        endif;
    endwhile;
endif;

标签: phpadvanced-custom-fieldsaddslashes

解决方案


尝试转义中的双引号addcslashes

echo addcslashes($string, '\"');

目前还不清楚为什么它不能像你现在拥有的那样工作。这两种方法在这里都可以正常工作:

https://repl.it/repls/ElderlyConfusedObjects

使用str_replace

如果不需要的结果字符串如下所示:

<a href="\"https://google.com\"">Link</a>

您可以通过以下方式删除周围的双引号:

$string = str_replace(array('"\"', '\""'), '\"', $string);

推荐阅读