MyBB Community Forums

Full Version: Need regex help
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi
I need help
Regex is black magic for me

preg_replace("/\bworld\b/m","Peter","Hello world! World worldworld World.");

This regex replace words when find word is "world" and "World", this don't  check is first  letter of the word is big or small
I nedd some thing to:

When i put "world" this replace only world not  World

When i put 'World" this replace only this where First letter is  big

Str_replace is ok but replace when world is inside other word exworlds or worldworld

I need replace only q single words
I'm a little confused as to exactly what you need, but if I can take a guess, you need something that specifically replaces BOTH "World" and "world" the same, but ONLY if they are single words?

DO Replace "world world" with "Peter Peter"
DON'T replace "worldworld"?

If so, something like this might work:

$result = preg_replace("/\b(world|World)\b/", "Peter", "Hello world! World worldworld World.");

You could also use: 

$result = preg_replace("/\b(world)\b/i", "Peter", "Hello world! World worldworld World.");

Either would work the same. (The /i at the end makes the entire pattern case insensitive). (Edit: the second example would also match "wORLD" and other combinations. if you don't want this, use the first example. Toungue)

One thing that's important with Regex is the delimeter that begins and ends the pattern. Most people use either a #, %, or a / symbol, but the pattern must start and end with this same symbol. Otherwise, PHP will get confused with the pattern and read it incorrectly. (In the above example, you can see the /i at the end, but the /i is a modifier that makes it case-insensitive, and is not a part of the patern itself. PHP knows this because it's looking for the / that began the pattern, and when it finds the second /, it knows that the pattern is finished. )

E.g. preg_replace("/(put pattern here)/", ..., ...);
Thanks for yor help

This works how I need (I guss)


preg_replace("/\bworld\b/m","Peter","Hello world! World worldworld World.");
Small tip: you can use regex101.com to develop and test your regexp. Example: https://regex101.com/r/4alEi8/1/

And I think the global flag could be usefull, I didn't remember if it's the default whith preg_replace or not
(2021-01-18, 10:51 AM)Supryk Wrote: [ -> ]Thanks for yor help

This works how I need (I guss)


preg_replace("/\bworld\b/m","Peter","Hello world! World worldworld World.");

So you just need to replace "world" (or "World") but not "worldworld" or other variations?

(2021-01-18, 11:47 AM)Crazycat Wrote: [ -> ]Small tip: you can use regex101.com to develop and test your regexp. Example: https://regex101.com/r/4alEi8/1/

And I think the global flag could be usefull, I didn't remember if it's the default whith preg_replace or not

Great suggestion. That website is INSANELY useful.
I need replace world not World or wOrLd or other variations

Last thing
How to check if specific word like "world" is somewhere between bbcode tags php or code?
Preg_match_all()
But regex?
Pleas help

Using this code I don't want to replace betwen code and php bbcode tags

preg_replace("/\bworld\b/m","Peter","Hello world! World worldworld World.");