I kind of know how preg_replace
works but I am horrible with regex. Here is what I am trying to achieve. I have url:
http://localhost/Wiki/index.php?*title=***TEST***&action=edit&redlink=1*
I want to replace that url with this one:
http://localhost/Wiki/index.php/*Special:FormStart?page_name=***TEST***&form=Main*
while taking the title from the first url (which is in this ex. TEST) and putting it into the new url.
Here is what I am thinking so far
$pattern = '/^?titl开发者_JS百科e=\ Dynamic title goes here \&action=edit&redlink=1$/';
replace it with
$replaceW = '/^Special:FormStart?page_name=\ Dynamic title goes here \&form=Main$/';
I will be thankful if you get me started on this!
Try this...
preg_replace(
'/^http:\/\/localhost\/Wiki\/index\.php\?title=(.*(?=&))&action=edit&redlink=1$/',
'http://localhost/Wiki/index.php/Special:FormStart?page_name=$1&form=Main',
'http://localhost/Wiki/index.php?title=TEST&action=edit&redlink=1'
);
Note that (.*(?=&))
replaces the dynamic text by using a lookahead (for more information on this see http://ruby.about.com/od/regularexpressions/a/lookback.htm)
精彩评论