在变量中正确存储 DataURI

Proper Storing of DataURIs in a Variable

可能有更优雅的方式来表达标题,但这是我能想到的最好的方式,因为坦率地说,我觉得自己很傻,因为我不知道答案,甚至不得不问,但这就是你的方式学习了,不用丢人了。

我存储了 dataURI,例如:

data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADAAAAAwCAYAAABXAvmHAAAE5klEQVR4nO1ZS0hjVxg+9xEJVFAnCzcuurVC3XfTWpqMMTGJbrSFQumAxhERFdE4YK4Bo5kY8zAvo0434nKghW46pXRhH5sKBXHXdikyDjGK0ETj7f/f5KY3N68bySSh9YPP/5x7Xv9/znfOubkS8oAH/L9B1XtAnufVYNTZbBqYpCgqVaKuCgySxiwwBXVvpHXYt+hrKajn5+fjNzc3hGEYolarT1ZXV0NLS0t74FwSK4DjLQ6H48nCwsJkKpXqSafTQl2v19sKxQ0PIAcM4vr6+r2rq6uQzWZ7ur+//8nd3R1ZXFz8Hp73YDnLsoSmaQwKm/DyPuj6u03+drvd7Z2dnf1dXV2fazSaX2DmSSKR6Dk+Pn55cnLy8vLysgeftbW1/Qz1Puvo6ND5fL53iGz2EXXfA3Kgzp1O59jZ2VkQZxmJzkNgT+12+w6kb8u1b3gACAwCJPRjPB7/APM48y6X68NKziMaIaEC4MnS2toaQr0jIR1U4jyioZtYCtiw57hZEbe3t+dK2zXFCiC6u7uP0eIeWFlZ+Ulpu6ZZgZGRkcTh4WHV7ZpmBUiRM14JGGnm4OCAHx4e5pBDaIeGuCGgBa3FwlmAZrRmM2c2WzgTWpOJM5nM3CDawUHBGsEajUbBGsAaDECjgRswAAfQDnD6AaAerZ7r1+u5346Onp2/fi348ceffz3T6rSc7nE/8DGn1ek4nS5jtVod9+rVdyuiz3kSQv2Njo4qnyYxQ4lpXpop3qbog8zDmZkZwQerdbzk+F6vLy9fEAACr/O8cSQD8lKvs/VzSfxDSQLgC5pncnx5vcAplOtaCpWKlYyfQd4euJcICSk62D16yU1guftVPhZbrFSs07//phae5fDtpx3KKpabEVlZUQmJuKvJzFaPcsPKJZQfgCyRfhsBlNK/uF8q6FGRhETUfgVq0GGzSogvMn6pelIUSCjD7HFarwCqOMbkQdZXQhXOf2V9lJWQmMqcw/VaAekwFTexLC+TEC+KUcjXOoDip091g1QlodrcsMqhZBMrkpBY5dcnj/IqU8J1T7LvO7lWxUx+eUnty6+lyqgsIQkoaQk6gY5nH2a+HuT3WvWC3WOF5atEy0qFXlmGFsjkmPmxzTJMljRRscy/eUgzbMYWMNtWVUAog7dLlYQiVLLn+eUV3kb3Xrwgu3t7And2d0lsBxkj27FtEt1GRkkkGiXhCDJCQuEwCYbCJBQKkWAwRLa2gsAtEghsEX8gQHwBP/H5/cTr9xGvz0c2vV7i8W6SjU2wHrAeD3FveIS0OMPP3Rtk3fWcrK27yNraOnECV51rQGfBvmyK70IIcJydmJgQvrxFIhGWoqi0knbN9Jv4XmiaAGZnZ98X09PT071K2zVNACCh9qxF0660XdPsgcnJyQD8Fp7CNMMwgXA4PN1onxRjeXlZY7Va42NjYzwS03Nzc4p+fzZ8BWw226NEIvFDOp3upbI3I34VoWn695aWlr5AIBAv177uewA0TjscDs3U1FQf8MuLi4sjdF5whqZnkRgIBNGbTCaPYDW+GB8f/6hUf434Nkqdnp6ei//MEP+hAbp3wGUofLWC+6ANjB3K3gV+hc9AWiQWixUopqGnEDoOrxnfAD+GTWuHPI+MRqMc2D7g1xX7qIejUqCEJOMKL1/odL39eMAD/iv4B7EvTT8yUT9VAAAAAElFTkSuQmCC

通过简单地执行以下操作在变量中:$some_var = "INSERT_ABOVE"; 并且通常工作正常,但我在我现在正在处理的一段代码中发现代码随时中断变量=>datauri使用了我上面显示的方案,但删除了该行,修复了代码。

所以我不确定?为什么?这个特定的 datauri 正在破坏代码, "" QUOTATIONS 中的任何内容都不应该被视为文字字符串吗?是否有最佳实践或者我应该说推荐的方法将 datauris 存储为要通过代码使用的变量,这样它就不会中断?

任何想法 and/or 的建议将不胜感激,TIA。

PS。这不仅仅是因为末尾的反斜杠,还因为字符串本身没有得到尊重,现在它像这样存储到变量中,谢谢。由于标题最准确地描述了所取得的成就,我认为它更合适。

使用Heredoc string quoting。这是在 PHP 中表示字符串的另一种方式。它可以消除您结束引用被字符串数据转义的问题。

  $str = <<<EOD
        data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADAAAAAwCAYAAABXAvmHAAAE5klEQVR4nO1ZS0hjVxg+9xEJVFAnCzcuurVC3XfTWpqMMTGJbrSFQumAxhERFdE4YK4Bo5kY8zAvo0434nKghW46pXRhH5sKBXHXdikyDjGK0ETj7f/f5KY3N68bySSh9YPP/5x7Xv9/znfOubkS8oAH/L9B1XtAnufVYNTZbBqYpCgqVaKuCgySxiwwBXVvpHXYt+hrKajn5+fjNzc3hGEYolarT1ZXV0NLS0t74FwSK4DjLQ6H48nCwsJkKpXqSafTQl2v19sKxQ0PIAcM4vr6+r2rq6uQzWZ7ur+//8nd3R1ZXFz8Hp73YDnLsoSmaQwKm/DyPuj6u03+drvd7Z2dnf1dXV2fazSaX2DmSSKR6Dk+Pn55cnLy8vLysgeftbW1/Qz1Puvo6ND5fL53iGz2EXXfA3Kgzp1O59jZ2VkQZxmJzkNgT+12+w6kb8u1b3gACAwCJPRjPB7/APM48y6X68NKziMaIaEC4MnS2toaQr0jIR1U4jyioZtYCtiw57hZEbe3t+dK2zXFCiC6u7uP0eIeWFlZ+Ulpu6ZZgZGRkcTh4WHV7ZpmBUiRM14JGGnm4OCAHx4e5pBDaIeGuCGgBa3FwlmAZrRmM2c2WzgTWpOJM5nM3CDawUHBGsEajUbBGsAaDECjgRswAAfQDnD6AaAerZ7r1+u5346Onp2/fi348ceffz3T6rSc7nE/8DGn1ek4nS5jtVod9+rVdyuiz3kSQv2Njo4qnyYxQ4lpXpop3qbog8zDmZkZwQerdbzk+F6vLy9fEAACr/O8cSQD8lKvs/VzSfxDSQLgC5pncnx5vcAplOtaCpWKlYyfQd4euJcICSk62D16yU1guftVPhZbrFSs07//phae5fDtpx3KKpabEVlZUQmJuKv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