0

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">
  • I'm not sure I understand your question. Are you generating the HTML or are you parsing it and trying to replace some values? (As for attribute order: it doesn't matter.) – Mat Sep 25 '11 at 11:45
  • 2
    If you want to echo PHP variables in HTML, you do something like – Alex Sep 25 '11 at 11:48

3 Answers3

0
$exmpl = '<iframe width="AnyNumber" height="AnyNumber">';
$exmpl = preg_replace('/(<iframe.*?width=").*?"/',  "\${1}$argv[1]\"", $exmpl);
$exmpl = preg_replace('/(<iframe.*?height=").*?"/', "\${1}$argv[2]\"", $exmpl);
Armali
  • 18,255
  • 14
  • 57
  • 171
0

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

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

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

Supriya Pansare
  • 519
  • 1
  • 4
  • 11
0

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.

Melsi
  • 1,462
  • 1
  • 15
  • 21