简体   繁体   中英

how to use array in preg_replace

How to use array with preg_replace. I've tried this:

function regex($text){
$array = 'replace1','replace2';
$text = preg_replace(
            '#.$array.*#', '', $text );
    return $text;
}

You don't have an array anywhere in your code. Assigning to a variable with comma-separated values doesn't make an array, you need to call the array() function. And you can't interpolate an array into a string (not to mention that variables are only interpolated in double-quoted strings, but you used single quotes).

You need to make an array of regular expressions.

function regex($text) {
    $array = array('#replace1#', '#replace2#');
    $text = preg_replace($array, '', $text);
    return $text;
}

DEMO

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