SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Guru DeNasio's Avatar
    Join Date
    May 2001
    Posts
    830
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    PHP to Javascript

    Hello,

    Can someone show me how to code the following PHP code in Javascript?

    PHP Code:
    if (eregi("^\[image\].+\[\/image\]$",$answers[$qid][$aid]))
       {
      
    $HTML_Ouput .= preg_replace("'\[image\](.+?)\[\/image\]'is","<img \\1 align=middle style=\"margin:4px 4px;\">",ereg_replace(";"," ",$avalue));
      } 
    Ballot-Box.net - free polls for webmasters
    FormLog.com - free form processor

  2. #2
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,938
    Mentioned
    214 Post(s)
    Tagged
    12 Thread(s)
    Hi,

    JS will only execute once your PHP script has been parsed and the resultant HTML sent to the browser.
    As such, a one to one translation is not possible.

    However, if you could tell us what you are trying to do, then I'm sure a JS solution could be found.

  3. #3
    SitePoint Guru DeNasio's Avatar
    Join Date
    May 2001
    Posts
    830
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I want to program the code above in Javascript, so I want Javascript to do what the code above does. Instead of doing the above in PHP I want to do it in Javascript.
    Ballot-Box.net - free polls for webmasters
    FormLog.com - free form processor

  4. #4
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,938
    Mentioned
    214 Post(s)
    Tagged
    12 Thread(s)
    Good luck

  5. #5
    SitePoint Guru DeNasio's Avatar
    Join Date
    May 2001
    Posts
    830
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Pullo View Post
    Good luck
    Thanks, but I would prefer you help me out with my request.
    Ballot-Box.net - free polls for webmasters
    FormLog.com - free form processor

  6. #6
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,938
    Mentioned
    214 Post(s)
    Tagged
    12 Thread(s)
    Well, the thing is, you won't be able to translate that snippet one for one, drop it in your page and have it work.
    That is because PHP is parsed and the resultant HTML is sent to your browser. JavaScript then operates on the HTML that the browser receives.
    That's not to say that what you are trying to achieve with PHP cannot be done with JS, but your snippet provides nowhere near enough context.
    For example what do the variables $HTML_Ouput, $answers[$qid][$aid] and $avalue contain?

    The best way for us to help you is if you describe what you are trying to do and provide some HTML and possibly JS for us to work with.
    Also, a link to a page where we can see everything would be handy.

  7. #7
    SitePoint Guru DeNasio's Avatar
    Join Date
    May 2001
    Posts
    830
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What the variables $HTML_Ouput, $answers[$qid][$aid] and $avalue contain are not relevant for the problem. You should assume that in Javascript these variables contain the same info.

    The problem is as follows:

    how do I convert

    [image]src='http://www.url.com/pic.gif';height='10';width='15'[/image]

    to

    <img src='http://www.url.com/pic.gif' height='10' width='15' align=middle style="margin:4px 4px;">

    in Javascript. The code above does this for PHP but I do not know how to do this in Javascript.
    Ballot-Box.net - free polls for webmasters
    FormLog.com - free form processor

  8. #8
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,938
    Mentioned
    214 Post(s)
    Tagged
    12 Thread(s)
    Something like this maybe?

    Code:
    var string = "[image]src='http://www.url.com/pic.gif';height='10';width='15'[/image]",
        src = string.replace(/\[\/?image\]/g, "").replace(/;/g, " ");
        newString = "<img " + src + "align=middle style=\"margin:4px 4px;\">"
        
    console.log(newString);

  9. #9
    SitePoint Guru DeNasio's Avatar
    Join Date
    May 2001
    Posts
    830
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks!
    Ballot-Box.net - free polls for webmasters
    FormLog.com - free form processor


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
  •