1

I have some strings in the following formats:

this is a string
This is a string
This is a (string)
This is a  string

and i want a regex convert it to the following:

this_is_a_string

with no leading

I have the following using preg_replace which is getting me almost all of the way:

preg_replace('/[^A-Za-z]+/', '_', $string)

However, it converts the last ) to an underscore which is not acceptable for my uses. I could easily trim that off in a separate function but im wondering if its possible to do with a single regex?

1
  • It looks like you might be wanting to make a URL slug. If so, I bet that if you search for "regex to build a URL slug" you could find your answer. Commented Aug 23, 2013 at 19:52

3 Answers 3

2
$result = preg_replace('~[^A-Z]+([A-Z]+)(?:[^A-Z]+$)?~i', '_$1', $string);
Sign up to request clarification or add additional context in comments.

1 Comment

+1 just make it: strtolower(preg_replace('~[^A-Z]+([A-Z]+)(?:[^A-Z]+$)?~i', '_$1', $string));
1
$result = preg_replace('~([^a-zA-Z\n\r()]+)~', '_', $string);

Try it HERE

Be sure tehre's no trailing or leading spaces in your string or it will be replaced too... Use trim($string) to remove it

3 Comments

Actually, I deleted my answer. I was told that I wasn't covering most of the OP's issues. preg_replace is the best method to use. Thanks for the mention though, cheers.
@Fred-ii-, Ow, I didnt really checked your code, but the idea wasnt bad xD
True, however it did not address the parentheses aspect.
0

Regular expressions are a great tool, but there are some things they're not well suited for. Converting the case of characters is one thing regular expressions alone can't do. You could use the preg_replace_callback function to transform the matched text to lowercase, but this is actually pretty straight forward to accomplish by simply changing your logic around a bit.

Perhaps something like this instead:

$string = 'This is a (string)';
preg_match_all("/[a-zA-Z]+/", $string, $matches);
$string = strtolower(implode('_', $matches[0])); // this_is_a_string

4 Comments

Thanks for pointing that out to me earlier, I deleted my answer. Yours is obviously the correct method.
more practical if replace pattern with \w+
@revo yes I considered that (in fact that's how I wrote it originally), but I suspect OP is trying to make a slug for a URL that's written in Latin letters only.
But I saw formats are limited to 4 numbers & I'm in love with best shortest ways.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.