PHP 更正 TinyMCE URL 转换
PHP to Correct TinyMCE URL conversions
自定义 CMS 中使用的 TinyMCE,将 url 中的 & 符号转换为 html 实体,如下所示:
<img src="https://maps.googleapis.com/maps/api/staticmap?markers=15.44,-14.57&zoom=14&size=250x180&sensor=false" alt="map" />
这张图片导致错误"The Google Maps API server rejected your request. Invalid request. Missing the 'size' parameter."
我无法更改 TinyMCE 行为(无效)。
或者,我想在保存到数据库之前使用 PHP 查找替换所有 href / src。
如何在 PHP 中编写代码以在字符串 $post 中搜索 href / src url 并找到与符号 html 实体代码并将其替换为“&”?
我尝试了使用 preg_replace 的示例,但无法正常工作。
我成功了,希望它能帮助别人。
$str = '<a href="https://some.com/?m=1&z=2&s=3&r=f">some text</a><img src="https://maps.googleapis.com/maps/api/staticmap?markers=136.22,-186.74&zoom=14&size=250x180&sensor=false" alt="map" />';
$re = '/(href|src)=("[^"]*")/';
preg_match_all($re, $str, $matches);
foreach ($matches[0] as $match){
$conv = str_replace('&', '&', $match);
$str = str_replace($match, $conv, $str);
}
echo $str; // & in url converted to &
自定义 CMS 中使用的 TinyMCE,将 url 中的 & 符号转换为 html 实体,如下所示:
<img src="https://maps.googleapis.com/maps/api/staticmap?markers=15.44,-14.57&zoom=14&size=250x180&sensor=false" alt="map" />
这张图片导致错误"The Google Maps API server rejected your request. Invalid request. Missing the 'size' parameter."
我无法更改 TinyMCE 行为(无效)。
或者,我想在保存到数据库之前使用 PHP 查找替换所有 href / src。
如何在 PHP 中编写代码以在字符串 $post 中搜索 href / src url 并找到与符号 html 实体代码并将其替换为“&”?
我尝试了使用 preg_replace 的示例,但无法正常工作。
我成功了,希望它能帮助别人。
$str = '<a href="https://some.com/?m=1&z=2&s=3&r=f">some text</a><img src="https://maps.googleapis.com/maps/api/staticmap?markers=136.22,-186.74&zoom=14&size=250x180&sensor=false" alt="map" />';
$re = '/(href|src)=("[^"]*")/';
preg_match_all($re, $str, $matches);
foreach ($matches[0] as $match){
$conv = str_replace('&', '&', $match);
$str = str_replace($match, $conv, $str);
}
echo $str; // & in url converted to &