You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何处理字符串中的多个双引号?PHP短信模板存储问题求助

Hey there! Let's work through your questions and get your SMS template workflow sorted out smoothly.


1. Handling Multiple Double Quotes in a String

Dealing with strings that contain multiple double quotes in PHP is straightforward—you just need to pick the right syntax for your scenario:

  • Wrap the string in single quotes: This is the simplest approach if you don't need to insert variables. Double quotes inside single quotes don't require escaping at all:
    $template = 'Your verification code is "1234", valid for "5" minutes.';
    
  • Escape double quotes with a backslash: If you prefer double quotes for variable interpolation, add a \ before each double quote in the string:
    $username = "John";
    $template = "Hello \"{$username}\", your password is \"abc123\".";
    
  • Use HEREDOC/NOWDOC syntax: Ideal for long, multi-line templates (like SMS content). HEREDOC parses variables, while NOWDOC acts like single quotes (no variable parsing):
    // HEREDOC (parses variables)
    $template = <<<EOT
    Hello "{$m}",
    Your account details:
    Username: "{$m}"
    Password: "{$p}"
    EOT;
    
    // NOWDOC (no variable parsing, great for fixed static text)
    $template = <<<'EOT'
    Hello "user",
    Your account details:
    Username: "your-name"
    Password: "your-pass"
    EOT;
    

2. Optimizing Your PHP SMS Template Code

Looking at your existing code, here's how to safely retrieve and use the fixed SMS template from your database:

Step 1: Fetch the Template from the Database

First, confirm your test table has a column for the SMS template (let's assume it's named sms_template). Update your loop to pull that value:

$sql = "Select * from `test` where `flag` = '0'";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
    while($row = mysqli_fetch_assoc($result)) {
        $n = $row['number'];
        $m = $row['user'];
        $p = $row['pass'];
        // Fetch the fixed SMS template directly from the database
        $sms_template = $row['sms_template']; 

        // Initialize your SMS request here...
    }
}

As long as the template is stored correctly in the database (with unescaped double quotes), $sms_template will hold the exact text you need—no extra escaping is required at this stage.

Step 2: Populate the Template with Variables

If your template uses placeholders (like {username} or {password}), use str_replace() to inject your variables cleanly:

// Example template stored in DB: "Hello \"{username}\", your password is \"{password}\""
$sms_content = str_replace(
    ['{username}', '{password}'],
    [$m, $p],
    $sms_template
);

This keeps your template flexible and avoids messy, error-prone string concatenation.

Step 3: Send the SMS Safely

When sending the SMS (via an API or gateway), handle the string based on the service's requirements:

  • If the service accepts JSON, use json_encode()—it will automatically escape double quotes for you:
    $payload = json_encode([
        'phone' => $n,
        'message' => $sms_content
    ]);
    // Send $payload via curl or your preferred HTTP client
    
  • For plain text requests, most SMS gateways handle raw strings with double quotes fine, but you can use urlencode() if the service requires URL-encoded parameters.

Quick Security Check

Even though your template is fixed, always sanitize user-derived variables like $m and $p to avoid unexpected issues:

$m = trim($row['user']);
$p = trim($row['pass']);
// Optional: Filter out invalid characters
$m = filter_var($m, FILTER_SANITIZE_STRING);

内容的提问来源于stack exchange,提问作者Hozefa Kanchwala

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 06:32:58