PHP to Javascript

Hello,

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


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));
  }

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.

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.

Good luck :slight_smile:

Thanks, but I would prefer you help me out with my request.

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.

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.

Something like this maybe?

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);

Thanks!