Jan
Jan

Reputation: 397

Make mb_ereg_replace case insensitive and use other regex? (PHP)

I have a few regex patters which I want to use now for the multibyte preg_replace function. I already found out that mb_ereg_replace is not using separators:

PHP mb_ereg_replace not replacing while preg_replace works as intended

My question is now, after I got my mb_ereg_replace function worked with \b how can I make it also case insensitive? My actual code is:

$myTitle = 'Wie geht es dir';

$string = mb_ereg_replace('\bWie geht es dir\b/i', 'Hat geklappt ', $myTitle);

echo $string;

But with /i it is not working. So here are my questions:

  1. How can I use /i at mb_ereg_replace or how can I make the pattern case insensitive?

  2. I also need those patterns for mb_ereg_replace but have no idea...? - Can somebody please help me? - I have now idea of mb_ereg_replace...

    1. trim(preg_replace('/\s\s+/', ' ', str_replace("\n", " ", $string)))

    2. preg_replace('~\b(?:' . implode('|', $delete) . ')\b~i', '', $string);

    3. As descibed above, I need also a case insensitive pattern...

I would be very grateful about help :) Greetings and Thank You!

Upvotes: 0

Views: 1201

Answers (1)

JazZ
JazZ

Reputation: 4579

Use mb_eregi_replace() php function.

mb_eregi_replace — Replace regular expression with multibyte support ignoring case

Or use mb_ereg_replace() with the option i as last parameter :

$string = mb_ereg_replace('\bWie geht es dir\b', 'Hat geklappt ', $myTitle, 'i');

From php manual (mb_ereg_remplace() last parameter) :

option

Matching condition can be set by option parameter. If i is specified for this parameter, the case will be ignored. If x is specified, white space will be ignored. If m is specified, match will be executed in multiline mode and line break will be included in '.'. If p is specified, match will be executed in POSIX mode, line break will be considered as normal character. If e is specified, replacement string will be evaluated as PHP expression.

Hope it helps.

EDIT

For question #2 :

Yes, those patterns will work with mb_ereg_replace() and mb_eregi_replace(). Just no need to use delimiters in the patterns.

i.e. :

1. trim(preg_replace('/\s\s+/', ' ', str_replace("\n", " ", $string)));

Will be :

trim(mb_ereg_replace('\s\s+', ' ', str_replace("\n", " ", $string), 'i'));
// or
trim(mb_eregi_replace('\s\s+', ' ', str_replace("\n", " ", $string)));

2. preg_replace('~\b(?:' . implode('|', $delete) . ')\b~i', '', $string);

Will be :

mb_ereg_replace('\b(?:' . implode('|', $delete) . ')\b', '', $string, 'i');
// or
mb_eregi_replace('\b(?:' . implode('|', $delete) . ')\b', '', $string);

Upvotes: 3

Related Questions