dpjo15650 2019-07-23 20:44
浏览 190

如何在插入数据库之前将#更改为%23? [重复]

This question already has an answer here:

This question has not been asked before. That link above is no help.

When trying to pass TXTurl variable in URL everything after the hashtag does not pass as the variable because it is a fragment. I was doing some reading, and it the variable does pass as %23. I have been trying to change the # to a %23 before it goes into the database.

I have tried urldecode() urlencode() - I have tried the below myUrlEncode function I found.

function myUrlEncode($TXTurl) {
    $entities = array('%21', '%2A', '%27', '%28', '%29', '%3B', '%3A', '%40', '%26', '%3D', '%2B', '%24', '%2C', '%2F', '%3F', '%25', '%23', '%5B', '%5D');
    $replacements = array('!', '*', "'", "(", ")", ";", ":", "@", "&", "=", "+", "$", ",", "/", "?", "%", "#", "[", "]");
    return str_replace($entities, $replacements, urlencode($TXTurl));
}


try {
    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
    // set the PDO error mode to exception
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    // prepare sql and bind parameters
    $stmt = $conn->prepare("INSERT IGNORE INTO MyFAQlist (TXTurl, TXTlinkname) 
    VALUES (:TXTurl, :TXTlinkname)");
    $stmt->bindParam(':TXTurl', $TXTurl);
    $stmt->bindParam(':TXTlinkname', $TXTlinkname);
    myUrlEncode($TXTurl); 

    // insert a row 
    $TXTurl = "$TXTurl";
    // insert a row
    $TXTlinkname = "$TXTlinkname";
    $stmt->execute();

I want to replace # with %23 when inserting TXTurl into database.

Also Tried to use rawurlencode. ddint' work

$stmt = $conn->prepare("INSERT IGNORE INTO MyFAQlist (TXTurl, TXTlinkname) 
VALUES (:TXTurl, :TXTlinkname)");
$stmt->bindParam(':TXTurl', $TXTurl);
$stmt->bindParam(':TXTlinkname', $TXTlinkname);
$TXTurl = rawurlencode($TXTurl);
</div>
  • 写回答

1条回答 默认 最新

  • dongsaohu6429 2019-07-23 22:33
    关注

    You could encode them using rawurlencode:

    $url = 'http://my-url.com/with-a-hash#';
    echo rawurlencode($url); // http%3A%2F%2Fmy-url.com%2Fwith-a-hash%23
    

    PHP Documentation: https://www.php.net/manual/en/function.rawurlencode.php

    评论

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料