Panagiotis Sakellariou
Panagiotis Sakellariou

Reputation: 196

preg_replace specific word followed by random number

I'd like to replace a specific word followed by random number followed by specific symbol. I figured the correct php function would be preg_replace but I have no idea how to proceed.

what I have is a string that contains a joomla non-sef url which looks like that:

index.php?option=com_k2&Itemid=378&lang=el&tag=some-tag-name&task=tag&view=itemlist

and I need to remove the Itemid=378& part. The number 378 could be a different number at any time so I need to catch all numbers.

Any help would be greatly appreciated

Upvotes: 0

Views: 1064

Answers (2)

jospratik
jospratik

Reputation: 1674

$new_url_string = preg_replace('/Itemid=\d+&/', '', $the_url_string);

Upvotes: 3

Doron
Doron

Reputation: 3256

preg_replace('/^(.*)Itemid=\d+&?(.*)$/', '$1$2', 'index.php?option=com_k2&Itemid=378&lang=el&tag=some-tag-name&task=tag&view=itemlist');

Upvotes: 2

Related Questions