简体   繁体   中英

Using preg_replace how can I inlude apostrophes and quotes

I need to correct the links in some HTML and I can use preg_replace but am stumped as to how to use quotes (") and apostrophe (') for example, replacing...

href=http://
href="http://
href='http://
href=/
href="/
href='/

Without including quotes it looks like...

$pattern = "href=http://";
$replacement = "href=http://example.com/test.php?go=http://";
$new = str_ireplace($pattern, $replacement, $html);

But how do to code preg_replace in this instance?

$pattern = "href=http://'";
$replacement = "href=http://example.com/test.php?go=http://";

Also, a prioritized loop through the $pattern will be ideal.

$pattern = "/href=['\"]{0,1}(\/|http:\/\/)/";
$replacement = "http://example.com/test.php?go=\\1";
$new = preg_replace($pattern, $replacement, $html);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM