mga_ka_php Posted March 26, 2009 Share Posted March 26, 2009 how do i remove space from a string? because i'm using str_replace, there are some spaces which couldn't be removed? Quote Link to comment https://forums.phpfreaks.com/topic/151162-solved-some-space-cant-remove/ Share on other sites More sharing options...
Maq Posted March 26, 2009 Share Posted March 26, 2009 <?php $s = "tims gsd d aa...a s asd f"; $s = str_replace(" ", "", $s); echo $s; ?> Quote Link to comment https://forums.phpfreaks.com/topic/151162-solved-some-space-cant-remove/#findComment-794096 Share on other sites More sharing options...
mga_ka_php Posted March 26, 2009 Author Share Posted March 26, 2009 That's what i did, but there are still spaces left, i don't know why. is there a special character for space? <?php $s = "tims gsd d aa...a s asd f"; $s = str_replace(" ", "", $s); echo $s; ?> Quote Link to comment https://forums.phpfreaks.com/topic/151162-solved-some-space-cant-remove/#findComment-794098 Share on other sites More sharing options...
taquitosensei Posted March 26, 2009 Share Posted March 26, 2009 They're probably tabs try str_replace("\t","", $s); Quote Link to comment https://forums.phpfreaks.com/topic/151162-solved-some-space-cant-remove/#findComment-794101 Share on other sites More sharing options...
Maq Posted March 26, 2009 Share Posted March 26, 2009 This should work: $s = "tims gsd d aa...a s asd f"; $s = preg_replace('/\s*/' ,"" , $s); echo $s; ?> Quote Link to comment https://forums.phpfreaks.com/topic/151162-solved-some-space-cant-remove/#findComment-794104 Share on other sites More sharing options...
mga_ka_php Posted March 26, 2009 Author Share Posted March 26, 2009 it didn't work also, i tried viewing the source and found out that the space is in how do i remove that? This should work: <?php $s = "tims gsd d aa...a s asd f"; $s = preg_replace('/\s*/' ,"" , $s); echo $s; ?> Quote Link to comment https://forums.phpfreaks.com/topic/151162-solved-some-space-cant-remove/#findComment-794109 Share on other sites More sharing options...
Maq Posted March 26, 2009 Share Posted March 26, 2009 $s = "tims gsd d aa...a s asd f"; $s = preg_replace('/\s*/' ,"" , html_entity_decode($s)); echo $s; ?> Quote Link to comment https://forums.phpfreaks.com/topic/151162-solved-some-space-cant-remove/#findComment-794111 Share on other sites More sharing options...
mga_ka_php Posted March 26, 2009 Author Share Posted March 26, 2009 still didn't work. this is giving me a headache. <?php $s = "tims gsd d aa...a s asd f"; $s = preg_replace('/\s*/' ,"" , html_entity_decode($s)); echo $s; ?> Quote Link to comment https://forums.phpfreaks.com/topic/151162-solved-some-space-cant-remove/#findComment-794116 Share on other sites More sharing options...
Maq Posted March 26, 2009 Share Posted March 26, 2009 still didn't work. this is giving me a headache. Where is this string coming from/generated? Quote Link to comment https://forums.phpfreaks.com/topic/151162-solved-some-space-cant-remove/#findComment-794117 Share on other sites More sharing options...
mga_ka_php Posted March 26, 2009 Author Share Posted March 26, 2009 from database still didn't work. this is giving me a headache. Where is this string coming from/generated? Quote Link to comment https://forums.phpfreaks.com/topic/151162-solved-some-space-cant-remove/#findComment-794123 Share on other sites More sharing options...
Maq Posted March 26, 2009 Share Posted March 26, 2009 Yeah sorry I'm not sure, here's a shot in the dark... $s = "tims gsd d a a...a s asd f"; $s = html_entity_decode($s); $s = preg_replace('/[\s|\t| ]*/' ,"" , $s); echo $s; ?> Quote Link to comment https://forums.phpfreaks.com/topic/151162-solved-some-space-cant-remove/#findComment-794128 Share on other sites More sharing options...
sastro Posted March 26, 2009 Share Posted March 26, 2009 Other alternative : eregi_replace(' ','',$str); Quote Link to comment https://forums.phpfreaks.com/topic/151162-solved-some-space-cant-remove/#findComment-794147 Share on other sites More sharing options...
mga_ka_php Posted March 26, 2009 Author Share Posted March 26, 2009 still didn't work. i'm getting this string from database to form like this id-name-state-city-date then use that as my url for my pages got the id, name, state, city, date from a site then save it in our database Yeah sorry I'm not sure, here's a shot in the dark... <?php $s = "tims gsd d a a...a s asd f"; $s = html_entity_decode($s); $s = preg_replace('/[\s|\t| ]*/' ,"" , $s); echo $s; ?> Quote Link to comment https://forums.phpfreaks.com/topic/151162-solved-some-space-cant-remove/#findComment-794151 Share on other sites More sharing options...
bluejay002 Posted March 26, 2009 Share Posted March 26, 2009 Was the data out from a single field or was it from a concatenated string? If the one you are trying to trim is from one field only, then instead of having that trim side on PHP, better to disallow it when you are trying to save it in the first place. I am not so sure how you saved it so am a bit confused if the previous suggestions were not working. Quote Link to comment https://forums.phpfreaks.com/topic/151162-solved-some-space-cant-remove/#findComment-794156 Share on other sites More sharing options...
Maq Posted March 26, 2009 Share Posted March 26, 2009 Can you give us an EXACT sample of a piece of data that doesn't work? Quote Link to comment https://forums.phpfreaks.com/topic/151162-solved-some-space-cant-remove/#findComment-794162 Share on other sites More sharing options...
sastro Posted March 26, 2009 Share Posted March 26, 2009 First, you should htmlentities the string and if the white space is not , just use array to str_replace. Quote Link to comment https://forums.phpfreaks.com/topic/151162-solved-some-space-cant-remove/#findComment-794170 Share on other sites More sharing options...
mga_ka_php Posted March 26, 2009 Author Share Posted March 26, 2009 i'm downloading the data from a site. by using curl stip the xml tags and separate the each tags with a delimiter ex. <name>my name</name><address>my address</address>..... then will convert that to name:my name|#|address:my address........ then save that to our database. if i'm going to get that data, i will save it as an array so i could get the key and value. now as im researching the data. i found out that in the name, it has space before and after the name so it will be like name: my name |#| i couldn't remove that space Quote Link to comment https://forums.phpfreaks.com/topic/151162-solved-some-space-cant-remove/#findComment-794178 Share on other sites More sharing options...
mga_ka_php Posted March 26, 2009 Author Share Posted March 26, 2009 i've been playing around with htmlentities and str_replace but still no luck. First, you should htmlentities the string and if the white space is not , just use array to str_replace. Quote Link to comment https://forums.phpfreaks.com/topic/151162-solved-some-space-cant-remove/#findComment-794181 Share on other sites More sharing options...
mga_ka_php Posted March 26, 2009 Author Share Posted March 26, 2009 solve my problem. a friend of mine suggested to convert it to hex. then i get the hex of space and remove it. and it works. thank you for help guys. i appreciated your suggestions. Quote Link to comment https://forums.phpfreaks.com/topic/151162-solved-some-space-cant-remove/#findComment-794191 Share on other sites More sharing options...
Maq Posted March 26, 2009 Share Posted March 26, 2009 solve my problem. a friend of mine suggested to convert it to hex. then i get the hex of space and remove it. and it works. thank you for help guys. i appreciated your suggestions. I still think that one of the ways we suggested would work... Quote Link to comment https://forums.phpfreaks.com/topic/151162-solved-some-space-cant-remove/#findComment-794429 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.