0

I have returned a webpage through curl_exec into a string. I am trying to then replace all hyperlinks with the string javascript:void().

The regex expression that I have tested at http://regexhero.net/tester/ is

(?<=href=("|'))[^"']+(?=("|'))

This works perfectly. When I try and use it in PHP with preg_replace(), I get the error : Unknown mofifier '['

Code snippet is

$pattern = "(?<=href=(".'"'."|'))[^".'"'."']+(?=(".'"'."|'))";
$replacement = "javascript:void();";
$result = preg_replace($pattern,$replacement,$result) ;

I also tried just escaping the string like

$pattern = "(?<=href=(\"|'))[^\"']+(?=(\"|'))";

I am scripting in a Win environment, but it is to go on LAMP.

Thanks for your input

1
  • 1
    Is your regex too complicated? Can't you do href=["'](\s*?)['"] Commented Dec 4, 2012 at 2:08

1 Answer 1

1

You need to add delimiters to your pattern. In JavaScript these are the / before and after the pattern. You can use the same for PHP, but I like using ~ instead.

$pattern = "~(?<=href=(".'"'."|'))[^".'"'."']+(?=(".'"'."|'))~";
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.