开发者

How do i use preg_replace to replace text around title in url?

开发者 https://www.devze.com 2023-02-19 14:44 出处:网络
I kind of knowhow preg_replace works but I am horrible with regex. Here is what I am trying to achieve. I have url:

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)

0

精彩评论

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