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 10 of 10
  1. #1
    Senior Coder
    Join Date
    Jun 2008
    Location
    Cornwall
    Posts
    2,099
    Thanks
    297
    Thanked 12 Times in 12 Posts

    Exclamation removing a specific part of a string?

    Hi All,

    i have a string which equals "SONY VAIOTT21JNB" and i have managed to remove the sony part of the string by having an array of words and checking the string and removing them if found, i have added vaio to the list but it wont remove it because it is joined to another word. so my question is how can i remove the vaio part of this string?


    here is what i have done to remove the sony part

    PHP Code:
                    $brand_array = array("HP","Packard Bell imedia""Packard Bell","Packard Bl","Compaq","Advent","Centurion","iXtreme","Sony""epson","Presario","Acer","philips","Toshiba","Pixma","Brother","Samsung","Canon","VAIO");
                            
                    
    $match false;
                    foreach(
    $brand_array as $v
                    {
                        if(
    preg_match('/'.preg_quote($v).'/i'$short_title)) 
                        {
                            
    $temp $v;
                            
    $short_title preg_replace("/$temp/i"""$short_title);
    }

    many thanks
    Luke

  • #2
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    That code actually works for me as you describe it should.
    PHP Code:
    $temp $v
    // should probably be:
    $temp preg_quote($v);
    // or concatenate it like you do in the if() condition 

  • #3
    Senior Coder
    Join Date
    Jun 2008
    Location
    Cornwall
    Posts
    2,099
    Thanks
    297
    Thanked 12 Times in 12 Posts
    hmm strange, so your actually getting just the TT21JNB from the string using the example code above?

    if this is the case then i wonder why im not? i have changed the code as you suggested above but no change.

    cheers for your reply
    Luke

  • #4
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    Yes, I was getting that result, and I get the same result with this shortened version:
    PHP Code:
    $short_title "SONY VAIOTT21JNB";
    $brand_array = array("HP","Packard Bell imedia""Packard Bell","Packard Bl","Compaq","Advent","Centurion","iXtreme","Sony""epson","Presario","Acer","philips","Toshiba","Pixma","Brother","Samsung","Canon","VAIO");
    $short_title preg_replace('/' implode('|'array_map('preg_quote'$brand_array)) . '/i'''$short_title);
    echo 
    $short_title// outputs: TT21JNB 

  • Users who have thanked Inigoesdr for this post:

    LJackson (02-28-2010)

  • #5
    Senior Coder
    Join Date
    Jun 2008
    Location
    Cornwall
    Posts
    2,099
    Thanks
    297
    Thanked 12 Times in 12 Posts
    hi mate, the above code works a treat thanks for that, still not sure why it didnt work in the first example for me?

    i have a little more code which i use to trim the strings and that is
    PHP Code:
                        $short_title preg_replace("/.{0}(windows).*/i"""$short_title) . "\n";
                        
    $short_title trim($short_title); 
    is it possible to incooperate the above into your example? so that i dont have unnecessary lines of code when it can be shortened

    thanks mate
    Luke
    Last edited by LJackson; 02-28-2010 at 08:25 PM.

  • #6
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    What is the first line doing? Unless I'm missing something, it won't do anything that won't be undone by the trim(). Here is how you can add the trim:
    PHP Code:
    $brand_array = array("HP","Packard Bell imedia""Packard Bell","Packard Bl","Compaq","Advent","Centurion","iXtreme","Sony""epson","Presario","Acer","philips","Toshiba","Pixma","Brother","Samsung","Canon","VAIO");
    $short_title preg_replace('/' implode('|'array_map('preg_quote'$brand_array)) . '/i'''trim($short_title)); // trim the subject before replacing matches
    echo $short_title

  • #7
    Senior Coder
    Join Date
    Jun 2008
    Location
    Cornwall
    Posts
    2,099
    Thanks
    297
    Thanked 12 Times in 12 Posts
    hi mate,

    the first line looks for the word "windows" in the string and deletes everything after it and a certain number of chars before (which is defined by the number currently set to 0)

    so if i had a title "my favourite windows are stain glass"

    the code would output "my favourite " if the 0 was a 3 it would output "my favouri"

    cheers
    Luke

  • #8
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    Ok, I was more asking why you can't use your regular array for that, but it is a slightly different use case I see. You can either run that line first:
    PHP Code:
    $short_title preg_replace("/.{0}(windows).*/i"""$short_title);
    $brand_array = array("HP","Packard Bell imedia""Packard Bell","Packard Bl","Compaq","Advent","Centurion","iXtreme","Sony""epson","Presario","Acer","philips","Toshiba","Pixma","Brother","Samsung","Canon","VAIO");
    $short_title preg_replace('/' implode('|'array_map('preg_quote'$brand_array)) . '/i'''trim($short_title)); // trim the subject before replacing matches
    echo $short_title
    Or do it a slightly more complicated way:
    PHP Code:
    $brand_array = array("HP","Packard Bell imedia""Packard Bell","Packard Bl","Compaq","Advent","Centurion","iXtreme","Sony""epson","Presario","Acer","philips","Toshiba","Pixma","Brother","Samsung","Canon","VAIO");
    $short_title preg_replace('/' implode('|'array_merge(array('.{0}(windows).*'), array_map('preg_quote'$brand_array))) . '/i'''trim($short_title));
    echo 
    $short_title

  • Users who have thanked Inigoesdr for this post:

    LJackson (02-28-2010)

  • #9
    Senior Coder
    Join Date
    Jun 2008
    Location
    Cornwall
    Posts
    2,099
    Thanks
    297
    Thanked 12 Times in 12 Posts
    nice one mate!!

    one last thing, at the moment with your code the string is being trimmed before the replacing how would i change the code so that it would trim it after the replacements have been made?

    thanks
    Luke

  • #10
    Senior Coder
    Join Date
    Jun 2008
    Location
    Cornwall
    Posts
    2,099
    Thanks
    297
    Thanked 12 Times in 12 Posts
    ok think ive solved it

    PHP Code:
    $short_title trim(preg_replace('/' implode('|'array_merge(array('.{0}(windows).*'), array_map('preg_quote'$brand_array))) . '/i'''$short_title)); 
    seems to work

    cheers for all your help mate!
    Luke


  •  

    Posting Permissions

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