...

View Full Version : preg replace help please.



LJackson
09-08-2009, 04:49 PM
I have this code

$short_title = preg_replace("/.{2}(-inch|\"|"| inch | Inch).*/", "", $alt_title) . "\n";


which works a treat, but how do i get it to work if i want to remove everything after a . and 2 places before?

i have tried this

$short_title = preg_replace("/.{2}(\.\").*/", "", $alt_title) . "\n";


which doesnt do anything i would like for example

this string
Samsung HT-TZ325R 5.1ch Home Cinema System - Black

cut down to this
Samsung HT-TZ325R

any help appreciated
Luke

SKDevelopment
09-08-2009, 05:50 PM
which works a treat, but how do i get it to work if i want to remove everything after a . and 2 places before?

I think using a regular expression could be a little bit an overkill for this. At least if you would like to remove everything after a . and 2 places before. String functions are usually considered much faster ...


<?php
$str = 'Samsung HT-TZ325R 5.1ch Home Cinema System - Black';

$str = substr($str,0,strpos($str,'.')-2);

echo $str; // outputs "Samsung HT-TZ325R"
?>

If I have not understood the problem correctly, could you explain please. I would be glad to provide any help of course.

SKDevelopment
09-08-2009, 06:04 PM
Still if you would really like to do it with a regular expression and preg_replace, I would give an example


<?php
$str = 'Samsung HT-TZ325R 5.1ch Home Cinema System - Black';

$str = preg_replace('/^(.*?)(?=.\.).*$/','$1',$str);

echo $str;
?>

I used a positive assertion (?=) (http://www.php.net/manual/en/regexp.reference.assertions.php) here.

Still I would recommend to use String functions as described in the previous post. Regular expressions for such a task could be an overkill.

LJackson
09-08-2009, 06:11 PM
Hi SKDevelopment,

thanks for your replys :)

didnt relise this could be done with string functions, thank you

Luke

LJackson
09-08-2009, 11:46 PM
one other question,

how can i remove part of a string for example if i had this

Joop Jump for Men 100ml Aftershave or this
Joop Jump For Men 100ml Aftershave or this
Paul Smith for Women 100ml EDP Perfume

and remove just the (for Men, For Men, for Women, For Women) so i had this


Joop Jump 100ml Aftershave or this
Joop Jump 100ml Aftershave or this
Paul Smith 100ml EDP Perfume

thanks.

p.s the strings will vary but most will have either for men or for women.

thanks
Luke

SKDevelopment
09-09-2009, 09:32 AM
If "for Men", "For Men", "for Women", "For Women" could be met in the string only once, you could use:


<?php
$str = 'Joop Jump for Men 100ml Aftershave or this';

$str = str_replace(array('for Men ','For Men ','for Women ','For Women '),'',$str);

echo $str; // outputs "Joop Jump 100ml Aftershave or this"
?>

Please notice that this will replace all occurrences of "for Men", "For Men", "for Women", "For Women" in the string.

Or you could use the case-insensitive version:


<?php
$str = 'Joop Jump for Men 100ml Aftershave or this';

$str = str_ireplace(array('For Men ','For Women '),'',$str);

echo $str; // outputs "Joop Jump 100ml Aftershave or this"
?>

Please notice that this would replace ll occurrences of "For Men", "For Women" in the case-insensitive way. So e.g. "FoR WoMeN" would be also replaced.

If there are some more complicated strings and you would like to have more complicated replacement algorithm for them, please ask questions. I would be glad to provide any help.

LJackson
09-09-2009, 02:03 PM
Hi mate, thanks for this,

hopefully it wont get any more complicated than this lol, what im doing is grabbing aftershave/perfume products from an amazon feed and comparing them to other stores for my price comparision feature but for some reason they include for him and for her right in the middle of the string so that when searching other stores for that item im not finding any, but without that its finding results :)

that should be it mate, thanks for your help...

i may be back if i require any more help though :)

cheers
Luke

SKDevelopment
09-09-2009, 06:48 PM
It is a pleasure. Yes, of course, please ask any questions. I would be glad if I could help.

LJackson
09-09-2009, 09:40 PM
Will do thanks mate!!!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum