Quovadisqc
Quovadisqc

Reputation: 91

in need of a php preg_replace regex

I am writing a script that needs to download images related to a product ID array to an external website. Here are the possible product ID combinations.

I need to be able to convert them to their URL equivalent on the manufacturer's website, which are (In the same order):

I am looking for a Regex to use with preg_replace that would do the trick. Thanks in advance!

Upvotes: 1

Views: 43

Answers (2)

Casimir et Hippolyte
Casimir et Hippolyte

Reputation: 89567

$output = strtolower(preg_replace('~\d\K(?=[A-Z])|-~', '_', $input));

\K removes that is matched on the left from the match result, so , the digit before the letter is not a part of the match and will not be replaced.

(?=...) is a lookahead assertion that checks if a letter if following, it isn't a part of the match result too and will not be replaced too.

Upvotes: 1

thelolcat
thelolcat

Reputation: 11545

I'm a noob in regular expressions but I`ll give it a shot.

Input: /([A-Z]+)\d+([A-Z]+)\-([A-Z]+)/

A-Z matches uppercase alpha characters \d matches numbers

"+" is used to repeat

And in the replacement callback use strtolower on the matches and join them how you want :P

Upvotes: 0

Related Questions