Results 1 to 4 of 4

Thread: Replace variables in php

  1. #1
    Join Date
    Oct 2009
    Posts
    67

    Replace variables in php

    Hi
    I have created a php program. In my php page I retrieve the data from the mysql database like this.
    Code:
    <object type="asd" class="abc" width="{wid}" height="{heig}" name="xyz"></object>
    I want the php to automatically replace those {wid} & {heig}, I want it because they are already declared in the php file as $wid=100 $heig=100. Any help or suggestion on this. Thanks in advance.
    Last edited by John Wilson; 04-12-2009 at 02:08 PM.

  2. #2
    Join Date
    May 2008
    Posts
    2,012

    Re: Replace variables in php

    Hi
    I have posted a part of code, just check if it works for you.
    Code:
    <object type="asd" class="abc" width="<?php echo $wid;?>" height="<?php echo $heig;?>" name="xyz"></object>
    Or you can just try this, if the above does not work.
    Code:
    <object type="asd" class="abc" width="'.$width.'" height="'.$height.'" name="xys"></object>
    Hope this will work with you. Any problem please do post back.

  3. #3
    Join Date
    Oct 2009
    Posts
    67

    Re: Replace variables in php

    Hi
    Thanks for the reply
    But this is not what I was looking for. What I want is the {wid} and I only have to use it nothing else, not the other thing which you have mentioned. By the way str_replace can work in this solution, no sure though. Need to check it out. Any other ideas of how to solve it.

  4. #4
    Join Date
    Apr 2008
    Posts
    1,948

    Re: Replace variables in php

    Hi
    You may want to try out something like this.
    Code:
    $template = <= Data from the database
    
    $template = str_replace("{wid}",$wid,$template);
    $template = str_replace("{heig}",$heig,$template);
    str_replace works with arrays to
    Code:
    $template = <= Data from the database
    
    $search = array{"{wid}","{heig}"};
    $rep = array{$wid,$heig};
    
    $template = str_replace($search,$rep,$template);
    Any problem do post back.

Similar Threads

  1. What are the server variables in PHP?
    By Lilya in forum Software Development
    Replies: 5
    Last Post: 07-01-2011, 01:13 PM
  2. What are the PHP Variables?
    By shivendra in forum Software Development
    Replies: 5
    Last Post: 28-01-2010, 07:05 PM
  3. Flash Variables to ASP
    By Mindstorm in forum Software Development
    Replies: 3
    Last Post: 31-10-2009, 06:07 PM
  4. Variables in .bat files
    By jackz in forum Software Development
    Replies: 3
    Last Post: 30-07-2009, 10:33 PM
  5. c# environment variables
    By EULALIA in forum Software Development
    Replies: 3
    Last Post: 13-07-2009, 02:23 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,579,435.20653 seconds with 17 queries