langemarkdesign Posted April 21, 2010 Share Posted April 21, 2010 I am working on a search engine script, and I have come across a problem. When a user searches for something in quotes I want to treat it the same way that Google does. So what I need to do is replace any spaces between the quotes with + signs. I have spent hours trying to figure this out. Anyone have any thoughts? Quote Link to comment https://forums.phpfreaks.com/topic/199223-i-need-to-replace-a-spaces-between-quotes-with/ Share on other sites More sharing options...
grim1208 Posted April 21, 2010 Share Posted April 21, 2010 $search_string = "hello world"; $new_search_string = preg_replace(" ", "+", $search_string); echo $new_search_string //should print out hello+world click here to view php manual on how to use preg_replace Quote Link to comment https://forums.phpfreaks.com/topic/199223-i-need-to-replace-a-spaces-between-quotes-with/#findComment-1045588 Share on other sites More sharing options...
langemarkdesign Posted April 21, 2010 Author Share Posted April 21, 2010 $search_string = "hello world"; $new_search_string = preg_replace(" ", "+", $search_string); echo $new_search_string //should print out hello+world click here to view php manual on how to use preg_replace Thanks, but this doesn't answer my question. I have a string like this... $string = 'How to "make chicken stew" if you have "no stew"' And I need it to print like this... How to "make+chicken+stew" if you have "no+stew" But ideally like this... How to make+chicken+stew if you have no+stew Quote Link to comment https://forums.phpfreaks.com/topic/199223-i-need-to-replace-a-spaces-between-quotes-with/#findComment-1045591 Share on other sites More sharing options...
grim1208 Posted April 21, 2010 Share Posted April 21, 2010 $a = "make chicken stew"; $a = preg_replace(" ", "+",$a); // replaces spaces in $a with + $b = "no stew"; $b = preg_replace(" ","+",$b); // replaces spaces in $b with + $string = "How to make $a if you have $b"; // $string = How to make+chicken+stew if you have no+stew i may be misunderstanding what you are asking. but how i am interpretting it (spelling sorry) is you are looking to make spaces into +'s Quote Link to comment https://forums.phpfreaks.com/topic/199223-i-need-to-replace-a-spaces-between-quotes-with/#findComment-1045598 Share on other sites More sharing options...
langemarkdesign Posted April 21, 2010 Author Share Posted April 21, 2010 Yes, but only the spaces that a between a set of quotation marks. All the other spaces I need to remain spaces. Quote Link to comment https://forums.phpfreaks.com/topic/199223-i-need-to-replace-a-spaces-between-quotes-with/#findComment-1045601 Share on other sites More sharing options...
grim1208 Posted April 21, 2010 Share Posted April 21, 2010 Yes, but only the spaces that a between a set of quotation marks. All the other spaces I need to remain spaces. my last post does just that Quote Link to comment https://forums.phpfreaks.com/topic/199223-i-need-to-replace-a-spaces-between-quotes-with/#findComment-1045603 Share on other sites More sharing options...
oni-kun Posted April 21, 2010 Share Posted April 21, 2010 my last post does just that What if there was a magical string that, I don't know, was a dynamic string. Quote Link to comment https://forums.phpfreaks.com/topic/199223-i-need-to-replace-a-spaces-between-quotes-with/#findComment-1045607 Share on other sites More sharing options...
langemarkdesign Posted April 21, 2010 Author Share Posted April 21, 2010 Yes, but only the spaces that a between a set of quotation marks. All the other spaces I need to remain spaces. my last post does just that Yes... But... Only if I have a string already separated into multiple strings. That's not what I have. I have one string that contains some words surrounded by quotes, and others that are not. The number of groups of words surrounded by quotes, and the number not, could be any number the user specifies. I need to take a variable string and replace only the spaces between quotes and leave all the rest alone. Quote Link to comment https://forums.phpfreaks.com/topic/199223-i-need-to-replace-a-spaces-between-quotes-with/#findComment-1045608 Share on other sites More sharing options...
grim1208 Posted April 21, 2010 Share Posted April 21, 2010 preg_replace() even str_replace() will help you with what you are looking for take the code example i gave you, and integrate it into your search query. i can't write out the code for you, don't know what you have placed already or what your variable setup is or anything. do you have script you can provide as to be more helpful? Quote Link to comment https://forums.phpfreaks.com/topic/199223-i-need-to-replace-a-spaces-between-quotes-with/#findComment-1045610 Share on other sites More sharing options...
langemarkdesign Posted April 21, 2010 Author Share Posted April 21, 2010 It is a search engine. When you go to Google and search for something with quotes surrounding it, Google uses the text in quotes as one keyword. So all I am trying to do is split the search query into keywords, while keeping any text in quotes as one keyword. Here's an example... $q = $_GET['q']; //Replace spaces between quotes with plus signs here, so that when we split into array below words surrounded by quotes stay together $keywords = split(" ", $q); Seems like a fairly straight forward question. Lets start simpler... I need to remove all words between quotes. I have seen a function to remove string between two characters before, but can't find it now, of course. Now, if we can do that... rather than removing the string, how can we just replace the spaces in the string with plus signs? Quote Link to comment https://forums.phpfreaks.com/topic/199223-i-need-to-replace-a-spaces-between-quotes-with/#findComment-1045616 Share on other sites More sharing options...
salathe Posted April 21, 2010 Share Posted April 21, 2010 Rather than splitting only on space, which is not what you want, how about a more specific (and complicated) splitting procedure? $string = 'How to "make chicken stew" if you have "no stew"'; $split = preg_split('/"([^"]*)"|\s/', $string, NULL, PREG_SPLIT_DELIM_CAPTURE|PREG_SPLIT_NO_EMPTY); print_r($split); That will output : Array ( [0] => How [1] => to [2] => make chicken stew [3] => if [4] => you [5] => have [6] => no stew ) Quote Link to comment https://forums.phpfreaks.com/topic/199223-i-need-to-replace-a-spaces-between-quotes-with/#findComment-1045629 Share on other sites More sharing options...
langemarkdesign Posted April 21, 2010 Author Share Posted April 21, 2010 $string = 'How to "make chicken stew" if you have "no stew"'; $split = preg_split('/"([^"]*)"|\s/', $string, NULL, PREG_SPLIT_DELIM_CAPTURE|PREG_SPLIT_NO_EMPTY); print_r($split); That is exactly what I needed! Thank you so much! Quote Link to comment https://forums.phpfreaks.com/topic/199223-i-need-to-replace-a-spaces-between-quotes-with/#findComment-1045916 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.