...

View Full Version : removing a specific part of a string?



LJackson
02-28-2010, 08:38 PM
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


$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

Inigoesdr
02-28-2010, 08:50 PM
That code actually works for me as you describe it should.

$temp = $v;
// should probably be:
$temp = preg_quote($v);
// or concatenate it like you do in the if() condition

LJackson
02-28-2010, 09:00 PM
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

Inigoesdr
02-28-2010, 09:06 PM
Yes, I was getting that result, and I get the same result with this shortened version:

$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

LJackson
02-28-2010, 09:23 PM
hi mate, the above code works a treat :D 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

$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

Inigoesdr
02-28-2010, 09:36 PM
What is the first line doing? Unless I'm missing something, it won't do anything that won't be undone by the trim() (http://php.net/trim). Here is how you can add the trim:

$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;

LJackson
02-28-2010, 09:50 PM
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

Inigoesdr
02-28-2010, 09:56 PM
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:

$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:

$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;

LJackson
03-01-2010, 12:20 AM
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

LJackson
03-01-2010, 12:39 AM
ok think ive solved it


$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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum