Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Oct 2009
    Location
    Finland
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Replacing special letters/marks etc. from a string

    Heya,

    So, I have this little problem - replacing special things (can't remember the correct word right now) from a string. This is to show links in more understandable way, example:

    "this_file_is_awesome.php", translating to "this file is awesome".

    This far I have gone around it with this little code:

    PHP Code:
    $filec1 str_replace(".php"""$file1);
    $filec2 str_replace("axaxa"""$filec1);
    $filec3 str_replace("oxoxo"""$filec2);
    $filec4 str_replace("_"" "$filec3);
    //Show string without garbage
    echo $filec4
    But Im afraid it is quite uneffective way, is there a better way to do this? Right now my current code doesn't even have half of what it should, and having 25 different valuables would be like, bad?

    I dont want ready code, but someone showing direction is what I'm looking for .

  • #2
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by Haaskis View Post
    Heya,

    So, I have this little problem - replacing special things (can't remember the correct word right now) from a string. This is to show links in more understandable way, example:

    "this_file_is_awesome.php", translating to "this file is awesome".

    This far I have gone around it with this little code:

    PHP Code:
    $filec1 str_replace(".php"""$file1);
    $filec2 str_replace("axaxa"""$filec1);
    $filec3 str_replace("oxoxo"""$filec2);
    $filec4 str_replace("_"" "$filec3);
    //Show string without garbage
    echo $filec4
    But Im afraid it is quite uneffective way, is there a better way to do this? Right now my current code doesn't even have half of what it should, and having 25 different valuables would be like, bad?

    I dont want ready code, but someone showing direction is what I'm looking for .
    PHP Code:
    $reppairs = array("/.php/" => "",
                                
    "/axaxa/" => "",
                                
    "/oxoxo/" => "",
                                
    "/_/" => " ");
    $what array_keys($reppairs);
    $with array_values($reppairs);
    $result preg_replace($what$with$initial); 
    order of pairs in $reppairs is important.

    best regards

  • #3
    New to the CF scene
    Join Date
    Oct 2009
    Location
    Finland
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you, oesxyl.

    I played and researched around a bit more with arrays and ended up doing this;

    PHP Code:
    $result $create;
                            
    $match = array(
                                
    "/ /",
                                
    "//",
                                
    "//",
                                
    "//",
                                
    "//",
                                );
                            
    $replace = array(
                                
    "_",
                                
    "axax",
                                
    "oxox",
                                
    "AXAX",
                                
    "OXOX",
                                );
                                
    $result preg_replace($match$replace$result); 
    $create is the original string I want to change.

    Works perfectly for my purposes.

  • #4
    Super Moderator
    Join Date
    Feb 2009
    Location
    England
    Posts
    539
    Thanks
    8
    Thanked 63 Times in 54 Posts
    I'd like to be a little pedantic and make the following point:

    From: http://php.net/manual/en/function.preg-match.php
    Do not use preg_match() if you only want to check if one string is contained in another string. Use strpos() or strstr() instead as they will be faster.
    Consider using str_replace or str_ireplace instead - you can still use the arrays like you did in the preg_replace.

    http://uk2.php.net/manual/en/function.str-replace.php
    http://uk2.php.net/manual/en/function.str-ireplace.php
    lamped.co.uk :: Design, Development & Hosting
    marcgray.co.uk :: Technical blog

  • #5
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by ComputerX View Post
    I'd like to be a little pedantic and make the following point:

    From: http://php.net/manual/en/function.preg-match.php


    Consider using str_replace or str_ireplace instead - you can still use the arrays like you did in the preg_replace.

    http://uk2.php.net/manual/en/function.str-replace.php
    http://uk2.php.net/manual/en/function.str-ireplace.php
    and is better to write 7 or more str..., or a loop, then one preg_replace?

    best regards


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •