0

基本上我收到这个:

<iframe width='425' height='350' frameborder='0' scrolling='no' marginheight='0' marginwidth='0' src='https://maps.google.com.br/maps?f=q&amp;source=s_q&amp;hl=pt-BR&amp;geocode=&amp;q=R.+Alm%C3%A9ria,+242+-+Penha,+S%C3%A3o+Paulo+-+S%C3%A3o+Paulo&amp;aq=0&amp;oq=Rua++Almeria&amp;sll=-14.239424,-53.186502&amp;sspn=79.602428,135.263672&amp;t=h&amp;ie=UTF8&amp;hq=&amp;hnear=R.+Alm%C3%A9ria,+242+-+Penha,+S%C3%A3o+Paulo,+03654-000&amp;z=14&amp;ll=-23.524401,-46.518859&amp;output=embed'></iframe>

我需要做的是将 width='' 引号或双引号内的任何值替换为另一个值。

我不擅长正则表达式,试图通过这个来学习似乎很复杂。

任何想法?

谢谢。

4

2 回答 2

0

不久前我回答了一个类似的问题->如何使用 php 替换 iframe html 标记的宽度和高度?

更好地使用str_replace(),它更容易,特别是如果您对正则表达式不满意。

于 2012-07-13T14:07:04.380 回答
0

想办法做到这一点,现在 425 和 350 可以是任何值,并用单引号或双引号引起来:

<?php

$embed = "<iframe width=\"425\" height=\"350\" frameborder='0' scrolling='no' marginheight='0' marginwidth='0' src='https://maps.google.com.br/maps?f=q&amp;source=s_q&amp;hl=pt-BR&amp;geocode=&amp;q=R.+Alm%C3%A9ria,+242+-+Penha,+S%C3%A3o+Paulo+-+S%C3%A3o+Paulo&amp;aq=0&amp;oq=Rua++Almeria&amp;sll=-14.239424,-53.186502&amp;sspn=79.602428,135.263672&amp;t=h&amp;ie=UTF8&amp;hq=&amp;hnear=R.+Alm%C3%A9ria,+242+-+Penha,+S%C3%A3o+Paulo,+03654-000&amp;z=14&amp;ll=-23.524401,-46.518859&amp;output=embed'></iframe><br /><small><a href='https://maps.google.com.br/maps?f=q&amp;source=embed&amp;hl=pt-BR&amp;geocode=&amp;q=R.+Alm%C3%A9ria,+242+-+Penha,+S%C3%A3o+Paulo+-+S%C3%A3o+Paulo&amp;aq=0&amp;oq=Rua++Almeria&amp;sll=-14.239424,-53.186502&amp;sspn=79.602428,135.263672&amp;t=h&amp;ie=UTF8&amp;hq=&amp;hnear=R.+Alm%C3%A9ria,+242+-+Penha,+S%C3%A3o+Paulo,+03654-000&amp;z=14&amp;ll=-23.524401,-46.518859' style='color:#0000FF;text-align:left'>Exibir mapa ampliado</a></small>";

var_dump($embed);

$new = preg_replace("/\bwidth='(.*?)'/", "width='100'", $embed);

$new2 = preg_replace("/\bheight='(.*?)'/", "height='100'", $new);

$new3 = preg_replace("/\bwidth=\"(.*?)\"/", "width='100'", $new2);

$new4 = preg_replace("/\bheight=\"(.*?)\"/", "height='100'", $new3);

var_dump($new4);

?>
于 2012-07-13T14:46:20.753 回答