-1

I'm trying to replace the window.location link function to window.open in my template..

The HTML looks like:

<div onclick="window.location="http://www.website.com/""</div>

I've tried following examples from the below links without any luck...

Add _blank to all external links Using str_replace( )

What should I use str_replace or preg_replace?

Community
  • 1
  • 1
hyp0thetical
  • 300
  • 6
  • 19
  • 3
    First of all fix your HTML, quotes are wrong there, either escape them or use single quotes – Mr. Alien May 06 '14 at 11:09
  • You have to use `preg_replace`, since you're not just doing a simple string replacement, you need to match the pattern `window.location="blah"` and add something before and after it. – Barmar May 06 '14 at 11:09
  • refer this to know the difference http://stackoverflow.com/questions/5245513/php-preg-replace-preg-match-vs-php-str-replace – Shafeeq May 06 '14 at 11:14

1 Answers1

0

Use preg_replace:

$new_line = preg_replace('/window.location=([\'"][^"\']*[\'"])/', 'window.open($1)', $line);
Barmar
  • 741,623
  • 53
  • 500
  • 612