jakebur01 Posted February 6, 2008 Share Posted February 6, 2008 I have the variable $title which information from four columns together: <?php $new_title=str_replace(' ',',',$title) ?> <meta name="keywords" content="<?php echo $new_title; ?>"> My problem is that I am winding up with too many commas in between phrases. It is inserting a comma in between every space, I just want one comma in between words. <meta name="keywords" content="HU-108C,CARBURETOR,,,,,,,RK21HU,KIT,TILLOTSON,CARBURETOR"> Quote Link to comment https://forums.phpfreaks.com/topic/89790-solved-str_replace-question/ Share on other sites More sharing options...
kenrbnsn Posted February 6, 2008 Share Posted February 6, 2008 What does the original string look like? Ken Quote Link to comment https://forums.phpfreaks.com/topic/89790-solved-str_replace-question/#findComment-460076 Share on other sites More sharing options...
The Little Guy Posted February 6, 2008 Share Posted February 6, 2008 Try This: <?php $new_title=preg_replace('/\s\s+/',',',$title); ?> Quote Link to comment https://forums.phpfreaks.com/topic/89790-solved-str_replace-question/#findComment-460078 Share on other sites More sharing options...
jakebur01 Posted February 6, 2008 Author Share Posted February 6, 2008 now it looks like this: <meta name="keywords" content="HU-108C CARBURETOR,RK21HU KIT TILLOTSON CARBURETOR"> Quote Link to comment https://forums.phpfreaks.com/topic/89790-solved-str_replace-question/#findComment-460084 Share on other sites More sharing options...
The Little Guy Posted February 6, 2008 Share Posted February 6, 2008 <?php $new_title=preg_replace('/\s\s+/',',',$title); $new_title=str_replace(' ',',',$new_title); ?> Quote Link to comment https://forums.phpfreaks.com/topic/89790-solved-str_replace-question/#findComment-460087 Share on other sites More sharing options...
jakebur01 Posted February 6, 2008 Author Share Posted February 6, 2008 wow! that worked great. Thank you! <meta name="keywords" content="105698,COMBO,120GPEA041,91VG,045,RETAIL,PKG,COMBO,COMBO"> Quote Link to comment https://forums.phpfreaks.com/topic/89790-solved-str_replace-question/#findComment-460090 Share on other sites More sharing options...
kenrbnsn Posted February 6, 2008 Share Posted February 6, 2008 If you're original string looks like $str = 'HU-108C CARBURETOR RK21HU KIT TILLOTSON CARBURETOR'; you can do something like: <?php $title = 'HU-108C CARBURETOR RK21HU KIT TILLOTSON CARBURETOR'; $new_title = implode(',',array_filter(explode(' ',$title),'nospace')); function nospace($str) { return(!($str == '')); } ?> <meta name="keywords" content="<?php echo $new_title; ?>"> Ken Quote Link to comment https://forums.phpfreaks.com/topic/89790-solved-str_replace-question/#findComment-460093 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.