一、问题场景
最近遇到一个需要将a系统中的数据存入b系统的需求,其中的答案部分是text类型数据,里面存放有图片、标签还有附件等数据,两个系统的富文本编辑器中,相同数据对应展示使用的标签并不一样,所以在保存的时候需要做一个替换。其中涉及到标签判断和数据拆分、关键属性的字段提取,以及将需要的属性插入新标签中等操作:
二、问题数据参考
a系统:
<img src=\"http://xxxxxxxx:3101/file/knowledge/20220212/1492396025694916609.png?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=admin%2F20220212%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20220212T071154Z&X-Amz-Expires=7200&X-Amz-SignedHeaders=host&X-Amz-Signature=1a43e9888176a5e2e354db622d0840346297254eaf0d88c8f84c3fde1a5e1011\" style=\"width: auto;max-height: 200px;margin: 5px 0;\">
<a class=\"custom-link\" contenteditable=\"false\" href=\"https://www.baidu.com\" style=\"cursor: default;margin:0 4px\" target=\"_blank\">百度</a>
<a class=\"custom-link\" contenteditable=\"false\" href=\"http://xxxxxxxxx:3101/file/knowledge/20220212/1492396188442300417.pdf?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=admin%2F20220212%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20220212T071233Z&X-Amz-Expires=7200&X-Amz-SignedHeaders=host&X-Amz-Signature=751d68fc63df2c5cfcd87ed0c70c4508bb9e5eb012ca3b0840675c3dca94deaa\" style=\"cursor: default;margin:0 4px\" target=\"_blank\" download=\"测试.pdf\">测试.pdf</a>
b系统:
<slot type=\"image\" value=\"http://xxxxxxxxx:3101/file/aiadver/20220210/1491709591258402818.jpg?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=admin%2F20220212%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20220212T070036Z&X-Amz-Expires=1200&X-Amz-SignedHeaders=host&X-Amz-Signature=3373728d0d6723c070fdf7c6720f41466894d40012a6d8738fc9f3aae4ec3000\" />
<slot type=\"link\" value=\"baidu\" name=\"百度1\" />
<slot type=\"file\" value=\"http://xxxxxxxxx:3101/file/aiadver/20220215/1493477606085890050.pdf?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=admin%2F20220215%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20220215T064943Z&X-Amz-Expires=7200&X-Amz-SignedHeaders=host&X-Amz-Signature=178671577ee691055b1f39362ae3f23df426c0f0a66844cd0d23346e09d66add\" name=\"测试.pdf\" />\n
三、进行转换的代码
/**
* 将问答同步发过来的图片标签
* @return void
*/
public static function imageTagTransform(string $content): string
{
$content = stripcslashes($content);
$awsInnerNet = config('aws.inner_net');//需要进行替换的部分1
$awsOuterNet = config('aws.outer_net');、、需要进行替换的部分2
$filePattern = '(http(s)?:\/\/' . "({$awsInnerNet}|{$awsOuterNet}\/file)" . '.*?)';
preg_match_all('/<img[\s\S]*?src="' . $filePattern . '"[\s\S]*?style="width: auto;max-height: 200px;margin: 5px 0;"[\s\S]*?>/i', $content, $matches);
if ($matches[0] && $matches[1]) {
foreach ($matches[0] as $key => $matchTag) {
$newImageTag = '<slot type="image" value="' . $matches[1][$key] . '"/>';
$content = str_replace($matchTag[$key], $newImageTag, $content);
}
}
return $content;
}
public static function linkTagTransform(string $content): string
{
$content = stripcslashes($content);
//先匹配出来字符串中所有的a标签(其中有链接和附件两部分内容)
preg_match_all('/<a class="custom-link" contenteditable="false" [\s\S]*?>[\s\S]*?<\/a>/i', $content, $matches);
$urlResources = $matches[0];
if (!empty($urlResources)) {
foreach ($urlResources as $urlString) {
// 将获取到的url数组分成链接和附件两个部分分别处理
preg_match_all('/<a class="custom-link" contenteditable="false" href="(.*?)".*?download="(.*?)">(.*?)<\/a>/i', $urlString, $match);
if ($match[0] ?? []) {
$slotString = '<slot type="file" value="' . ($match[1][0] ?? '') . '" name="' . ($match[2][0] ?? '') . '" />';
$content = str_replace($match[0][0], $slotString, $content);
continue;
}
preg_match_all('/<a class="custom-link" contenteditable="false" href="(.*?)".*?>(.*?)<\/a>/i', $urlString, $match);
if ($match[0] ?? []) {
$slotString = '<slot type="link" value="' . ($match[1][0] ?? '') . '" name="' . ($match[2][0] ?? '') . '" />';
$content = str_replace($match[0][0], $slotString, $content);
}
}
}
return $content;
}