Add Replacing url php
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
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 :
$comments = preg_replace ("`tinymce/`", "http://mysite.com/tinymce/", $comments);
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
expects the first parameter to be a regular expression
all 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/@
Use str_replace
.
$comments = str_replace ("tinymce/", "http://mysite.com/tinymce/", $comments);
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.
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
精彩评论