Best approach to this using preg_replace/match

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • fartsniff

    Best approach to this using preg_replace/match

    i found this code out in the ng, and its seems long and clunky,
    i am still experimenting with preg_match and _replace, but the
    syntax is a bit confusing. it seems i always misplace or mistype
    something.

    this is the code that i am trying to replace with better, optimized code.
    i would like to make a preg_match and then preg_replace for this, and if
    a match is found, just removing it.

    i started with this:
    (preg_match ("/(\*\/)|(\/\*)|(\-\-)|(#|;)/", $line))
    and
    $line = preg_replace ("/(\*\/)|(\/\*)|(\-\-)|(#|;)/", "", $line) . ";";

    and this is the total items that i am searching for:
    $line = str_replace("." , " ", $line);
    $line = str_replace("\" ", " ", $line);
    $line = str_replace("'" , "", $line);
    $line = str_replace("+" , " ", $line);
    $line = str_replace("-", " ", $line);
    $line = str_replace("*" , " ", $line);
    $line = str_replace("/", " ", $line);
    $line = str_replace("!" , " ", $line);
    $line = str_replace("%" , " ", $line);
    $line = str_replace(">" , " ", $line);
    $line = str_replace("<" , " ", $line);
    $line = str_replace("^" , " ", $line);
    $line = str_replace("(" , " ", $line);
    $line = str_replace(")" , " ", $line);
    $line = str_replace("[", " ", $line);
    $line = str_replace("]", " ", $line);
    $line = str_replace("{" , " ", $line);
    $line = str_replace("}" , " ", $line);
    $line = str_replace("\\ ", " ", $line);
    $line = str_replace("=" , " ", $line);
    $line = str_replace("$" , " ", $line);
    $line = str_replace("#" , " ", $line);
    $line = str_replace("?" , " ", $line);
    $line = str_replace("~" , " ", $line);
    $line = str_replace(":" , " ", $line);
    $line = str_replace("_" , " ", $line);
    $line = str_replace(" ", " ", $line);
    $line = str_replace("&a mp;", " ", $line);
    $line = str_replace("&c opy;", " ", $line);
    $line = str_replace("&n bsp;", " ", $line);
    $line = str_replace("&q uot;", " ", $line);
    $line = str_replace("&u uml;", "ü", $line);
    $line = str_replace("&U uml;", "Ü", $line);
    $line = str_replace("&" , " ", $line);
    $line = str_replace(";" , " ", $line);
    $line = str_replace("\n ", " ", $line);

    should i just leave all of the str_replaces ? or can preg handle all of
    these ?
    thanks.


  • Jason Dumler

    #2
    Re: Best approach to this using preg_replace/match

    fartsniff wrote:[color=blue]
    > i found this code out in the ng, and its seems long and clunky,[/color]

    Yeah, but sometimes you gotta take the long and clunky road
    [color=blue]
    > this is the code that i am trying to replace with better, optimized code.[/color]

    See my comment below about arrays.
    [color=blue]
    > i would like to make a preg_match and then preg_replace for this, and if
    > a match is found, just removing it.[/color]

    Since all the characters you are trying to replace are not part of a
    simple class of characters (\s is spaces, \d is digits, etc.)
    [color=blue]
    >[/color]

    <snip lots of code>
    [color=blue]
    >
    > should i just leave all of the str_replaces ? or can preg handle all of
    > these ?
    > thanks.
    >
    >[/color]

    According to the documentation, you can use arrays with str_replace.
    Something like this:

    <?
    $search = array("+", "-", "&amp;");
    $replace = array(" ", " ", " ");

    $result = str_replace($se arch, $replace, $line);
    // or, if they are all replaced with the same thing
    $result = str_replace($se arch, " ", $line);
    ?>

    You build a big array at first, but it's not so hard to maintain, once
    you get the initial array in place. This *seems* less clunky...

    Jason

    Comment

    Working...