开发者

simple PHP preg_replace question

开发者 https://www.devze.com 2022-12-18 00:46 出处:网络
Here\'s a simple preg_replace $string = \'src=\"index.php\'; $string = preg_replace(\"/(src=(\'|\\\")+[^(http:|https:)])/i\", \"src=\\\"http://domain.com/\", $string);

Here's a simple preg_replace

$string = 'src="index.php';
$string = preg_replace("/(src=('|\")+[^(http:|https:)])/i", "src=\"http://domain.com/", $string);
echo $string;

I expect the result to be src="http://do开发者_开发百科main.com/index.php but it turns out to be src="http://domain.com/ndex.php.

I must be missing something here..


That's a really messed up regex. What are you trying to achieve exactly? It looks like if the URL doesn't start with http or https you want to add the domain? If so, you're quite a bit off:

$string = preg_replace('/src=(\'|")?(?!htts?:)/i', 'src="http://domain.com/');

should be a lot closer to the mark.

What does this regex do? It looks for:

  • src=
  • optionally followed by either ' or "
  • not followed by http: or https:
  • all done case insensitive

Note: {?!...) is called a negative lookahead and is one example of a zero-width assertion. "Zero-width" here means that it doesn't consume any of the input. In this case it means "not followed by ...".

What does your regex do? It looks for:

  • src=
  • one or more ' or " characters
  • any one character that isn't any of (http:|https:) (that's what the [^...] construct means)
  • all case insensitive

Note:

[^(http:|https:)]

is equivalent to:

[^():https]

meaning any character that is not one of those characters.


The construct [^(http:|https:)] is incorrect. It matches any character except (, h, t, p, :, |, s, or ).


$string = 'src="index.php'; $string = preg_replace("/src=('|\")?(?!htts?:)/i", "src=\"http://domain.com/", $string); echo $string;

try this: I tested it myself

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号