PHP: Replace specified tag values in PHP [duplicate]
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);
精彩评论