Php 替换iframe的字符串值

Php 替换iframe的字符串值,php,str-replace,Php,Str Replace,我有这样的绳子 $string = ' <iframe width="560px" height="250px" src="https://www.google.com/maps/embed/v1/place?key=AIzaSyBdVKAGo41VFI44444440l17aXhg&q=Space+Needle,Seattle+WA" allowfullscreen></iframe>'; $newstring = '<iframe width="100

我有这样的绳子

$string = ' <iframe width="560px" height="250px" src="https://www.google.com/maps/embed/v1/place?key=AIzaSyBdVKAGo41VFI44444440l17aXhg&q=Space+Needle,Seattle+WA" allowfullscreen></iframe>';
$newstring = '<iframe width="100%" height="100%" src="https://www.google.com/maps/embed/v1/place?key=AIzaSyBdVKAGo41VFI44444440l17aXhg&q=Space+Needle,Seattle+WA" allowfullscreen></iframe>';
$string = ' <iframe width="560px" height="250px" src="https://www.google.com/maps/embed/v1/place?key=AIzaSyBdVKAGo41VFI44444440l17aXhg&q=Space+Needle,Seattle+WA" allowfullscreen></iframe>';

$s = preg_replace('/(width|height)="[0-9]*px"/', '$1="100%"', $string);
echo $s;
$string='';
我需要新字符串来替换数据属性,使其看起来像这样

$string = ' <iframe width="560px" height="250px" src="https://www.google.com/maps/embed/v1/place?key=AIzaSyBdVKAGo41VFI44444440l17aXhg&q=Space+Needle,Seattle+WA" allowfullscreen></iframe>';
$newstring = '<iframe width="100%" height="100%" src="https://www.google.com/maps/embed/v1/place?key=AIzaSyBdVKAGo41VFI44444440l17aXhg&q=Space+Needle,Seattle+WA" allowfullscreen></iframe>';
$string = ' <iframe width="560px" height="250px" src="https://www.google.com/maps/embed/v1/place?key=AIzaSyBdVKAGo41VFI44444440l17aXhg&q=Space+Needle,Seattle+WA" allowfullscreen></iframe>';

$s = preg_replace('/(width|height)="[0-9]*px"/', '$1="100%"', $string);
echo $s;
$newstring='';
我不知道数据的价值,但我知道它总是100%


如何做到这一点?

您可以尝试使用php Preg\u replace,它用于使用regex替换字符串部分

$string = preg_replace("/\"([0-9]*)px\"/si","'100%'",$string);
echo $string;

这也可以工作,但比
preg\u replace
长:

$string = ' <iframe width="560px" height="250px"src="https://www.google.com/maps/embed/v1/place?key=AIzaSyBdVKAGo41VFI44444440l17aXhg&q=Space+Needle,Seattle+WA" allowfullscreen></iframe>';
$pos = strpos($string, "src");
$newstring = substr_replace($string,' <iframe width="100%" height="100%" ',0,$pos);
$string='';
$pos=strpos($string,“src”);
$newstring=substr\u replace($string,使用
preg\u replace()
您可以这样做

$string = ' <iframe width="560px" height="250px" src="https://www.google.com/maps/embed/v1/place?key=AIzaSyBdVKAGo41VFI44444440l17aXhg&q=Space+Needle,Seattle+WA" allowfullscreen></iframe>';
$newstring = '<iframe width="100%" height="100%" src="https://www.google.com/maps/embed/v1/place?key=AIzaSyBdVKAGo41VFI44444440l17aXhg&q=Space+Needle,Seattle+WA" allowfullscreen></iframe>';
$string = ' <iframe width="560px" height="250px" src="https://www.google.com/maps/embed/v1/place?key=AIzaSyBdVKAGo41VFI44444440l17aXhg&q=Space+Needle,Seattle+WA" allowfullscreen></iframe>';

$s = preg_replace('/(width|height)="[0-9]*px"/', '$1="100%"', $string);
echo $s;

如果您只想更改html元素的高度和宽度,只需在html文档或php模板文件中包含一个好的旧css文件即可,无需正则表达式或dom解析

因为你的iframe看起来像是从google获得的,它没有类或id,所以你可以把它放在div或任何有id的地方,以防你在页面中有其他iframe

div#maps-iframe-container iframe{
    width: 100%;
    height: 100%;
}