Jump to content

I need to replace a spaces between quotes with +


langemarkdesign

Recommended Posts

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?

Link to comment
Share on other sites

$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

Link to comment
Share on other sites

$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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

 

 

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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
)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.