preg_replace地图嵌入的自定义模式

时间:2012-07-13 14:03:31

标签: php preg-replace expression

基本上我收到了这个:

<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 =''引号或双引号内的任何值替换为另一个值。

我对正则表达式并不擅长,试图通过这种方法学习似乎很复杂。

有什么想法吗?

感谢。

2 个答案:

答案 0 :(得分:0)

我不久前回答了类似的问题 - &gt; How to replace width and height an iframe html tag using php?

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

答案 1 :(得分:0)

找到一种方法,现在,425 ans 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);

?>