Speed up development with full-stack environments for every branch.

Learn More

Regular expression search and replace using mb_ereg_search and mb_ereg_replace [mbstring and PHP]

132 Runs 709 Views 199 Copies
Saved

Saved

gtsolutions 373

gtsolutions
published 4 years ago

<?php

/*
 * PHP mbstring - Regular expression search and replace using mb_ereg_search and mb_ereg_replace
 */

//Setup string and regular expression for a multibyte regular expression match
$str = "This is a demo string";
$pattern = "\w+";
//Setup string and regular expression for a multibyte regular expression match
mb_ereg_search_init($str, $pattern, "i");
//Multibyte regular expression match for predefined multibyte string
if (mb_ereg_search()) {
    //Retrieve the result from the last multibyte regular expression match
    $r = mb_ereg_search_getregs();
    print_r($r);
    do {
        //Returns the matched part of a multibyte regular expression
        $r = mb_ereg_search_regs();
    } while ($r);
}
echo "<br /><br />";


//Replace regular expression with multibyte support
$string = '<b>Demo "string"</b>';
$pattern = array('<', '>', '"', '\'');
$replacement = array('&lt;', '&gt;', '&quot;', '&#39;');
for ($i = 0; $i < sizeof($pattern); $i++) {
    $string = mb_ereg_replace($pattern[$i], $replacement[$i], $string);
}
echo $string;
    
?>
Please login/signup to get access to the terminal.

Your session has timed out.

Dismiss (the page may not function properly).