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"> 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 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); ?> 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"> 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); ?> 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"> 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 Link to comment https://forums.phpfreaks.com/topic/89790-solved-str_replace-question/#findComment-460093 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.