用preg_replace替换

时间:2014-04-24 18:33:28

标签: php regex svg preg-replace base64

我有一个SVG文件,其内嵌有一堆base64编码图像。我想用这些图像的路径替换这些数据。

显示base64编码数据的SVG路径:

<image overflow="visible" width="960" height="720" id="Layer_0_1_" xlink:href="
data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAA8EAAALRCAYAAAB76aEvAAAACXBIWXMAAB
KbAAASmwHgklkMAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAIiDZJREFUeNrsvQu
WIzmuLEi6FPmp6nl3ljJbnJXNVmYB75zpvn2rsjJDco5AEqQRDpB0uRQRWZ2qVitSH//wCwMMBv/1
85f/0zn3f7nOw3vv7n3Qb0e/P3J8eoQQDp3/6O+fffyjj975nadrCO69+19eI/7u+/fv7q+//nK
vrxfnfb0eej2/" />

到目前为止,我有以下PHP代码:

$content = preg_replace('/"data:image\/png;base64(.*)"/', '"pathtologo.png"', $content);

我意识到(。*)一定有什么问题,因为它没有替换任何东西。

有人可以就此事提供任何帮助吗?正则表达从未成为我的强项。

更换后,我期待:

<image overflow="visible" width="960" height="720" id="Layer_0_1_" xlink:href="
pathtologo.png" />

不能使用下面提供的正则表达式答案的base64数据的一小段摘录:

  

iVBORw0KGgoAAAANSUhEUgAAA8EAAALRCAYAAAB76aEvAAAACXBIWXMAABKbAAASmwHgklkMAAAA   GXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAIiDZJREFUeNrsvQuWIzmuLEi6FPmp   6nl3ljJbnJXNVmYB75zpvn2rsjJDco5AEqQRDpB0uRQRWZ2qVitSH // wCwMMBv / 185F / 0zn3f7nO   w3vv7n3Qb0e / P3J8eoQQDp3 / 60 + fffyjj975nadrCO69 + 19eI / 7U + / + fv7q // nKvrxfnfb0eej2 /   vLjff / 8 / 3LIs8cm / O + Ot69q / 99vj9g13vV5vr / vasrzezuGua / f7r6 + vnc9v13D1B / pvdan7VrNt   T6dTbAv + M18 / ffp0e744vzj3cmtHaj / + HrcxtyG1Eb3S + / Q33VP67u3HwTd9Itup3we3fgpX8 / F0   oOsajS / ZL / J4d7evGIva9eF4oyf + hscltyu1Mz3rb27tQ0OIxmBu6x8 / fsTxfrlczOvj42tz5y0f   O / nN7TJaf7DfuK1ofH758sWdz2ez / bn9sA / 4O9R + 9KSxjuMXxxX3Bf3u21 / fYrufT2f39evXeF76   HR2D + 5HnD88N + ozmQlyLbt / n49GDvstzj + cL9j8dm69LG1PYhvhdng90zOt6iXOIzvP58 + fYZvJY   + BseZ7gWYJvykz + nc9B45Puhe + R7onu / ffP273M5Fq8N9Bv6HPte3lNeQOP104OO / dtvv5W2l2sH   ZY ++ H / i8BieHf / lMrP88H + nY1FY0tmj5Wter2ma4 / vG9xXvO14VzFvsQxyo + rbnRnR + D9YnHZR0j   a9wjaRxwO2rHqfPttme6pfQnP3Ef4LHNTxwvl + TT / N ++ Z + MTx7O1b2L7nJbbmBLj0LI / rNc9tots   g9H6JD / f7DNLXMC6Y693nZ5aP881euJeN / P7EPfPcGh / E + BNR / ff4f4x8Xuc + 3vbd + B + j9oXz / Z9   I + YFZ + KPD3B9 / + + 85A d / X / 16 / Hr8jI / gysaLxh4adF0QHI6B4IRi + 5-6 / xoOOEA8 / 3R / JvWewGlk   5PyUw9AwhB / R1hLoSsP6SLu9xTjoX1 / ofu9R1zfjiOT / 5LXI69L6VgOy2hokx8lsO2mfJfCzkBnc   BRuW82SPk0NzasVjBPe3fuxpJwnwZgz0mfG9Z34 / awmV46qO7fddc / cAGO3vUR9YvxmtK3I8PK8N   jo3h0TU + AsAdOf5HslN + Pf6Wj // 7 / He4i16k6GfaSH899htl5CWk6I309M2C4BipvV76tlw09kI1   2dEgSEj6yB05v5xsMBx6ODlko93 + EhpjcmOO7XN7PeV2w3kk29E2qN1BQ8ir83d2zryFobHLKSKu   q9d2 / BuO5PB45rEsgZgWZXrEfHq2p7q3PkdPf7hs5vcOP5A5d61j4T1zxJmilwvMAxkxxrUE5wb2   izSs8Ry4bpHTDft6NqLFEWt6cOToel3c5ZqYJnRcGX2Ux0e2jBZZ4 + Pz9zj6y1FPvr9yHMHAsdrw   Ees9nqP + 223WtEfMBf4b + xqdrZpDQYvkS3aINmdGzo / R / JHriGTxYP / K + xutuZZTp573sSBFi9zK   + bUXRPUiuXvXt63Tiy66jShiP8w6OvbYjps + CE / HNJxi263r0 / aWvevIz2ibP / san8nE / Rke5 /复   gLfoxBm6473H / tkBrv8J + R + 3kEca1 / WHapBMTc47gxkBQPFysI3Sz0PHyu9Hkr3z + 64ZjRgwLi3D   + Ll9Oz5HbxP9COOzF20Yrh + KwcsAAsGxdc / VFF + zjqbOp40RzwY / 0ky1sViM4gyEes6BHuBgqifS   qCWIayNfoQFmeM3SOYfGMFId94BfbBsENwxO6S1KJ0CqMF8LA2XpkGDq8iaqKxwy / G + mJl8y7bW5   JgGqRxRg3cFY1z85nvA40vkQ3 / fhkIMnrp7CUSjPwekMvfFuOcEsR5gEpPfOH + 08 / MpzSLZb87uO   81SO + 3sA5MQKsptB05vfe9bne2y8TbqPYkTta5 + tA7s3VrTxN4q0zka67wG5M23 / CCbUM2z7X49f   jwiC78mr + GiPZ0 + Enx0Id6 / PjylU731 / 1aiyF + p7QXD8 / uIPtaN3H9eTNgIFCQcHFSTL + 5ZAY / A6   R + MPDeD3GHePPI8VETQNYcqHXcMu40FGRo7SoY + Mv6MgOEUxwpDibdFMfQZSGgieiQRrRj5G4jEK   jDmcDCwIUFKOL4NDBqHy2JgbjiB1NlIor6NpA2XtwyggnxfvyTJwZeRNRnf5N2XNdTZdf ++ 4stYd   PQJpg5Rd5 / DP / dYAonQ + zDgXtTGGThINvI6c8DOR4s28EA416 / 5yD06v7Y9m3sTxtOptMRstn82Z   dsZYPbRnZB + OzCGfiZDu2Y / QqdGsAQoIl + 17JKf22fbJ6PiP1rz42UDxI673aE7ue46NNwHBf7dB   89aD8Fck + O36YCSc0TNIbCM8i1vduzk5NxSmeCZd309et2aI9YCVNHZM8TIyIB + 0EWqRqREd8COu   F3upe73okkb1fCQt + HGE + mPHD80gnt2wS5vklAANNGvAQHMIodASGrBsQLNAGRuVDHQxyqu9TxFU   plnT31pkH + elXMdkhFxLbaC24ygzAlUZubTWSEl / 1hyL / DlGkPk7yzIW9eoLA2UgEfbNkx5gfvQ +   r40fjS49cgRofSDTULS9Q0bze + u71Ybm / YW + M + G9Rfesa + / NaesY99Kpe7aEFKZCZ1nDWJg8x2jP   lWJea1ifLoz17EjsW9iYbyFA + 6z985maH ++ dTvYhQPDfIRL87Os / CoRHnrh35fz / BJFguZk1m4NP   dOQgJ3z5284aDOV4kyDLQ9QXXsIoJ3hEZ /蒲式耳/ S + DG15 / J + 5soeiew2HveBh / V69 + / N7rwyyjQAUp   BhCpf9fxOQNsH01JfEbO2q6 + J5 + vXfGlkbpqMPpB5tFa945AkB7XNancMriU1HQGxTK6y2s9 / HS /   lxRrCS4RyGAkCYEyA6J6X / Te0uTpairlVsRw1oiS0egR2JuetzmazCJLVjR11gmqUbK7jhWxAVr5   xZgfLY / bywm22A17coJn6K4brQdj / mzuz / MeooMvrW0fu / Yucf + 28qutnOvZa5Lz8UhOsDYe41oB   qRSPEMTSfivvoTzdY4XV7vntM5mYo / 1pL36x9uiPDoR / 5UN / eBD8s9 / AIxbmZxrav9TtHte / ViSW   NlIWeQoAfmfyWoPfq74pjgnCGPc + RpHOR4xv6ZUe5fHtoZQeuzYX8wqlgMmeCPB7z7FeDtfU / STT   L6NwR / Ky3ttJONUIzmYDaPOuGDEiEtwrR2OBIsyfZRCJJZVkhJWfGIHlvzF3FkXO5H0hdVoz8mXU   E8ssIThP4lStQj5eN96fJpikrQ1av2rUfpm / LsH9I8SxpGgX5lmnv9djwnzg2LQow9JZIcfRTO6 +   BYL3CjvNggVJw7fvLz17qRX9FIOjtlObE6uxFkYKzbNOgnvWS80Z26xFNA5PweyLvU7GGWdqc0 / +   OMh89vr / EfN2fxYA / J8O0P82IPjogPvZKcmP9GTNHvseYPcRnQiWYVvHlC + eZCtfrKcKyHSm2T7a   HH / I + kd0tqNti / dufR8BZbOhu1Yxlq5V85JadFPZbPuNjRrpvleYa + / a8ozxOgOE7bxVPWL3kQSw   HRH + qffAviQQR1pjjqCbUuENYTsHrTXAihgynTnOkRwZkyJYVj1dnt94bK6viyAX + xbBDYKVbfmZ   SjvGGrWWg0iCJXleGbVWhaYGIBiPm9pr67jZNR + hA9XhIfoMHQrsZJDRMI3yfu8 + i20jnQ9WuoJ0

2 个答案:

答案 0 :(得分:1)

你可能想试试这个:

<?php
$html = <<< EOF
<image overflow="visible" width="960" height="720" id="Layer_0_1_" xlink:href="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAA8EAAALRCAYAAAB76aEvAAAACXBIWXMAABKbAAASmwHgklkMAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAIiDZJREFUeNrsvQuWIzmuLEi6FPmp6nl3ljJbnJXNVmYB75zpvn2rsjJDco5AEqQRDpB0uRQRWZ2qVitSH//wCwMMBv/185f/0zn3f7nOw3vv7n3Qb0e/P3J8eoQQDp3/6O+fffyjj975nadrCO69+19eI/7u+/fv7q+//nKvrxfnfb0eej2/" />
EOF;

$newimage = preg_replace('%"(data:image\/png;base64.*)"%sim', '"pathtologo.png"', $html);
echo $newimage ;
?>

<强> 样本:
http://ideone.com/E8wwAX

<强> 说明:

Options: Case insensitive; Exact spacing; Dot matches line breaks; ^$ match at line breaks; Greedy quantifiers; Regex syntax only

Match the character “"” literally «"»
Match the regex below and capture its match into backreference number 1 «(data:image\/png;base64.*?)»
   Match the character string “data:image” literally (case insensitive) «data:image»
   Match the character “/” literally «\/»
   Match the character string “png;base64” literally (case insensitive) «png;base64»
   Match any single character «.*?»
      Between zero and unlimited times, as few times as possible, expanding as needed (lazy) «*?»
Match the character “"” literally «"»

"pathtologo.png"

Insert the character string “"pathtologo.png"” literally «"pathtologo.png"»

答案 1 :(得分:1)

我为什么它不能正常工作的理论是因为点(.)匹配除了换行符\n以外的所有内容,我建议使用s修饰符来允许点到匹配换行符,或使用否定。

另外,您可能希望在?之后添加.*量词,以进行非贪婪的匹配。

$content = preg_replace('~"data:image/png;base64.*"~s', '"pathtologo.png"', $content);