添加替换 url php
你好,我想将以下短语替换
:
为
http://mysite.com/tinymce/jscripts/tiny_mce/ plugins/emotions/img/smiley-cool.gif
我已经尝试过:
$comments = preg_replace ("tinymce/", "http://mysite.com/tinymce/", $comments);
但我收到错误:
warning Delimiter must not be alphanumeric or backslash
你能帮助我吗? 谢谢
hello I want replace following phrase:
tinymce/jscripts/tiny_mce/plugins/emotions/img/smiley-cool.gif
with:
http://mysite.com/tinymce/jscripts/tiny_mce/plugins/emotions/img/smiley-cool.gif
I have tried :
$comments = preg_replace ("tinymce/", "http://mysite.com/tinymce/", $comments);
but i get an error:
warning Delimiter must not be alphanumeric or backslash
can you help me?
thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
preg_replace
函数的第一个参数必须是由您选择的字符分隔的正则表达式 (regex)。例如,您应该这样做:
您还可以使用输出缓冲(使用 ob_start)对所有 url 或您想要的任何内容应用重写功能。
http://fr.php.net/manual/en/function.ob -start.php
并尝试匹配一个真实的表达式,这里你可以使用str_replace,但如果你在注释中写tinymce/,那么它也会被替换。
The first parameter of
preg_replace
functions must be a regular expression (regex) delimited by a character of your choice.For example you should do :
You may also use output buffering (with ob_start) to apply rewrite function on all url or anything you want.
http://fr.php.net/manual/en/function.ob-start.php
And try to match a real expression, here you could use str_replace, but if you write tinymce/ in a comment then it will be replaced too.
preg_replace
期望第一个参数是正则表达式所有正则表达式都需要位于分隔符内,例如
/regex/
因此,如果您希望代码正常工作,则必须将正则表达式更改为
/tinymce\//
(并转义正斜杠)或使用不同的分隔符,例如@tinymce/@
preg_replace
expects the first parameter to be a regular expressionall regular expressions needs to be inside delimeters for example
/regex/
so if you want your code to work you have to change your regex to
/tinymce\//
(and escape the forward slash) or use a different delimeter like@tinymce/@
使用
str_replace
。只是要非常小心:这是一种非常原始的方法。例如,如果您运行它两次,它将替换已经正确的
http://mysite.com/tinymce/
中出现的内容,从而破坏该过程中的链接。Use
str_replace
.just be very careful with this: It's a very primitive method. For example, if you run it twice, it will replace the occurrence in the already correct
http://mysite.com/tinymce/
, breaking the link in the process.阅读 PHP 中正则表达式的基础知识(模式必须以定义的字符开始和结束,例如 @(之后是标志))或使用函数 str_replace
Read about basics of regular expressions in PHP (pattern has to begin and end with defined character ex. @ (after that come flags)) or use function str_replace