php - 在双引号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;
解决方案
尝试转义中的双引号addcslashes
:
echo addcslashes($string, '\"');
目前还不清楚为什么它不能像你现在拥有的那样工作。这两种方法在这里都可以正常工作:
↳ https://repl.it/repls/ElderlyConfusedObjects
使用str_replace
:
如果不需要的结果字符串如下所示:
<a href="\"https://google.com\"">Link</a>
您可以通过以下方式删除周围的双引号:
$string = str_replace(array('"\"', '\""'), '\"', $string);
推荐阅读
- java - 为什么 jps(java VM lister)会在 cygwin 中给出与 cmd.exe 不同的答案?
- html - 在导航栏 Bootstrap 的右侧对齐图标
- php - mysql:命令不同步;您现在无法运行此命令 -> 在 php 函数中运行查询时
- azure-web-app-service - 启用运行状况检查的 Azure Web 应用服务是否会在运行状况不佳的实例上对 CPU 进行横向扩展?
- intellij-idea - 我在使用带有 intellij 的场景生成器时遇到问题
- python - 如何查找numpy数组是否包含沿第三维的向量?
- reactjs - 在 useEffect 中发出调度
- objective-c - 通过 CFMessagePort 的 Cocoa/App Group 进程间通信
- html - 如何使用 nth-child inline 更改行的背景颜色?
- javascript - 如何检查电子邮件是否在 Firebase 中验证?