twyntee_trey
twyntee_trey

Reputation: 185

Regular Expression to remove underscore and string

I need to use PHP regex to remove '_normal' from the end of this url.

http://a0.twimg.com/profile_images/3707137637/8b020cf4023476238704a9fc40cdf445_normal.jpeg

so that it becomes

http://a0.twimg.com/profile_images/3707137637/8b020cf4023476238704a9fc40cdf445.jpeg.

I tried

$prof_img = preg_replace('_normal', '', $prof_img);

but the underscore seems to be throwing things off.

Upvotes: 1

Views: 2355

Answers (6)

goesta
goesta

Reputation: 669

You just forgot to add delimiters around your regex.

http://www.php.net/manual/en/regexp.reference.delimiters.php

When using the PCRE functions, it is required that the pattern is enclosed by delimiters. A delimiter can be any non-alphanumeric, non-backslash, non-whitespace character.

Often used delimiters are forward slashes (/), hash signs (#) and tildes (~). The following are all examples of valid delimited patterns.

$prof_img = preg_replace('/_normal/', '', $prof_img);

$prof_img = preg_replace('#_normal#', '', $prof_img);

$prof_img = preg_replace('~_normal~', '', $prof_img);

Upvotes: 1

NeverHopeless
NeverHopeless

Reputation: 11233

Using non-capturing groups, you can also try like this:

$prof_img = preg_replace('/(.+)(?:_normal)(.+)/', '$1$2', $prof_img);

It will keep the required part as a match.

Upvotes: 0

Fabian Tamp
Fabian Tamp

Reputation: 4516

As others have stated, str_replace is probably the best option for this simple example.

The problem with your specific code is that your regex string is undelimited, you need to this instead:

$prof_img = preg_replace('/_normal/', '', $prof_img);

See PCRE regex syntax for a reference.

The underscore is treated as a normal character in PCRE and isn't throwing things off.

If you require that only _normal at the end of the filename is matched, you can use:

$prof_img = preg_replace('/_normal(\.[^\.]+)$/', '$1', $prof_img);

See preg_replace for more information on how this works.

Upvotes: 7

Ja͢ck
Ja͢ck

Reputation: 173522

You can use decompose the URL first, perform the replacement and stick them back together, i.e.

$url = 'http://a0.twimg.com/profile_images/3707137637/8b020cf4023476238704a9fc40cdf445_normal.jpeg';

$parts = pathinfo($url);
// transform
$url = sprintf('%s%s.%s', 
     $parts['dirname'],
     preg_replace('/_normal$/', '', $parts['filename']),
     $parts['extension']
);

You might note two differences between your expression and mine:

  1. Yours wasn't delimited.

  2. Mine is anchored, i.e. it only removes _normal if it occurs at the end of the file name.

Upvotes: 0

Ryan
Ryan

Reputation: 1338

str_replace should work.

$prof_img = str_replace('_normal', '', $prof_img);

Upvotes: 3

Curtis Mattoon
Curtis Mattoon

Reputation: 4742

Try using str_replace; it's much more efficient than regex for something like this.

However, if you want to use regular expressions, you need a delimiter:

preg_replace('|_normal|','', $url);

Upvotes: 3

Related Questions