开发者

PHP: Replace specified tag values in PHP [duplicate]

This question already has answers here: Grabbing the href attribute of an A element (10 answers) Closed 9 years ago.

Take a look at the HTML example below:

<iframe width="AnyNumber" height="AnyNumber">

How can i replace AnyNumber开发者_运维问答 with specified number $1 and $2 for width and height? they can be swapped like:

 <iframe height="AnyNumber" width="AnyNumber">


It reminds me of templates.

For example you have you html files like this

<div class="nav">@navigationMenu</div>
    <img src="@imgSrc">
    <span>@title</title>
    <p>@article</p>

and then with a simple php class you replace with the desired values, for example:

$this->registry->main_layout->set('imgSrc', './img/green.jpg');  

There are many tutorials along with the source code (usually a single-simple php class) that implement this.


$exmpl = '<iframe width="AnyNumber" height="AnyNumber">';
$exmpl = preg_replace('/(<iframe.*?width=").*?"/',  "\${1}$argv[1]\"", $exmpl);
$exmpl = preg_replace('/(<iframe.*?height=").*?"/', "\${1}$argv[2]\"", $exmpl);


$exmpl = 'iframe width="AnyNumber" height="AnyNumber">';

$exmpl = str_replace('width="AnyNumber"','width="'.$1.'"',$exmpl);

$exmpl = str_replace('height="AnyNumber"','width="'.$2.'"',$exmpl);

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜