简体   繁体   English

如何用正则表达式中的点替换字符串的一部分?

[英]How to replace a portion of string with a dot in regex?

Here is the string that I want to replace:这是我要替换的字符串:

<img src="./handler_image.php?i=c52bc1c30f560f4a15f99eeb8c04fea6" alt="Favicon" class="favicon">

I wrote this code:我写了这段代码:

$answer = preg_replace('/<img src="\./.*?>/', '', $answer);

but it does not work.但它不起作用。 If I replace it with:如果我将其替换为:

$answer = preg_replace('/<img src=".*?>/', '', $answer);

It works but then it replaces all the images and not the ones whose src is in the format above.它可以工作,但随后会替换所有图像,而不是src为上述格式的图像。 How should I modify this statement?我应该如何修改这个声明?

The forward slash after the dot needs to be escaped too.点后的正斜杠也需要转义。

try this:尝试这个:

$answer = preg_replace('/<img src="\.\/.*?>/', '', $answer);

Here's the regex working <img src="\\.\\/.*?> .这是正则表达式工作<img src="\\.\\/.*?>

By the way, you can go there http://regexr.com/ to know how your regex is working.顺便说一下,你可以去http://regexr.com/了解你的正则表达式是如何工作的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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