1
votes

I want to replace all matching occurrences at start of the string. Here is a use case. www.example.com/example.com/http://example.com/www.abc.com/www.example.com/xyz.com/example.com should become www.abc.com/www.example.com/xyz.com/example.com

every matching occurrence should be removed until a non matching string encounters.

I am using preg_replace currently I have this regular expression

'/(http:\/\/){0,1}(www.){0,1}example.com(\/)*/i' The problem is that it replaces all matching strings.

I want to replace matched strings only at the start, if matching fails once after that it should not replace any thing. Any helping suggestions ?

Thanks.

4

4 Answers

3
votes

Use the ^ anchor to ensure it only matches at the beginning of the string:

'/^(http:\/\/){0,1}(www.){0,1}example.com(\/)*/i'

To learn more about anchors, check out this regex tutorial.

Note: In regular expressions, ? is the same as {0,1}.

1
votes

You could also capture the string instead of preg_replace:

(?:(?:www.|http::?\/\/)?example\.com\/)+(.*)

The only capture group contains the string you're looking for. See here.

Also, I noticed you had http:: in your input string. I matched it with same in the regex but with a ? just in case. And last thing, {0,1} might be written as ?. It's shorter :)

The PHP code (I'm not too familiar with the language, but I believe this should work):

preg_match('/(?:(?:www.|http::?\/\/)?example\.com\/)+(.*)/', $html, $matches);
$url = $matches[1];
0
votes

First of all PHP regex engine allows you to use an alternate regex delimiter. So you can use ~ or # instead and void escaping forward slashes. Also dot needs to be escaped since its a special regex character.

A refactored regex can be like this:

'~(?:https?://)?(?:www\.)?example\.com/~i'

Complete PHP code:

$regex_replace = '~(?:https?://)?(?:www\.)?example\.com/(?!$)~i';
$string = "www.example.com/example.com/http://example.com/www.abc.com/xyz.com/example.com/";
$after = preg_replace($regex_replace, '', $string);
var_dump($after);

OUTPUT:

string(32) "www.abc.com/xyz.com/example.com/"
-1
votes

Use the $limit parameter of preg_replace (third argument in this example):

preg_replace('/(http:\/\/){0,1}(www.){0,1}example.com(\/)*/i', $replacement, $subject, 1);