简体   繁体   中英

PHP regex solution - Remove some special characters and replace some with text

I have a PHP variable, say

$myvariable = "te    xt!@ na#@)(me+=&t^*ext?>;.'na^%me";

I want to replace special characters and group of special characters including blank space with a single underscore _ . The string may contain & and it may be replaced with and .

The result of previous variable should be;

te_xt_na_me_andt_ext_na_me

How can I do this in PHP?

This assumes, anything but "characters" is regarded disposable.

$patterns = array(
    '/&/'             => 'and',  // Ampersand to "and"
    '/[^[:alpha:]]+/' => '_'     // Anything *but* a character to underscore
);

$result = preg_replace(array_keys($patterns), array_values($patterns), $input);

The last pattern replaces groups of one or more occurences of "non-word" characters according to the current locale 1 (and thus including white-space).


1 Side-note (might be irrelevant): if the server the script runs on has en_US as locale, the following replacements occur:

$input = 'app!le___s &!   orän=%ges';
$result = 'app_le_s_and_or_n_ges';

If the locale is de_DE , this would be the result:

$result = 'app_le_s_and_orän_ges';

Because ä is part of [[:alpha:]] in this particular locale. The obvious solution to circumvent this would be to substitute the character class for [a-zA-Z] .

this should do it:

$myvariable = str_replace('&','and',$myvariable)
$myvariable = preg_replace ('/[^a-z]+/i', '_' , $myvariable)

see: http://php.net/manual/de/function.preg-replace.php

the caret (^) inside the squared brackets means to look for everything, that is not declared in the brackets. So every special character is not "az". The plus signalises, that multiple occurences should be matched. The 'i' behind the delimiting slash means to do a case-insensitive search.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM