Search code examples
phpstringimplode

PHP - How to turn an array like "eg1","eg2","eg3" into "+eg1","+eg2","+eg3"?


I am working on a search feature for my site.

At the moment I am taking a string from a single input and then cleaning it into a string like "eg1 eg2 eg3".

Here is the code for that :

$pattern = mysqli_real_escape_string($link, $_POST['search']);
$pattern = strip_tags($pattern); 
$pattern = trim ($pattern);
$pattern = explode(' ', $pattern);
$pattern = implode(" ", $pattern);

What i would like to do is take the original string and at either the point where it is in an array (after the explode) append + in front of every value.

I have been looking through the documentation on the functions I have been making use of but cannot quite seem to see how they could work in this way. This is being that i can turn the string to "eg1 +eg2 +eg3" but i cannot get the plus in front of the first result?

If anyone could point out the relevant function of possibly provide me with an example on how i could go about this task, it would be greatly appreciated!

Any input/suggestion/feedback is welcome, thank you!

Please don't flame and go RTM!!!!! Because i have and i cannot seem to find what i am looking for.


Solution

  • Just add the missing + as the first character in the resultant string:

    $pattern = "+".implode(" +", $pattern);