Scholli Scholli - 3 years ago 151
PHP Question

How to add slash after every string in PHP?

How i can add a slash or ; after every string. Here i have an Example:

This is what i have

"SKU" "TITLE" "LINK" "LINK2" "PRICE" "World of Warcraft"

But i will have like that

"SKU";"TITLE";"LINK";"LINK2";"PRICE";"World of Warcraft"

How i can made this? I work with affiliate and this Partner dont give
. Its only an
So, how i can add slash into every "string"?
i already tried to add
after every 2nd
. But it dont work with my code...
also tried with

str_replace(" ", ";", $item);

Answer Source

The solution using preg_replace function:

$str = '"61926182767182" "DAS GEILE GAME" "HTTP://" "HTTP://" "32.59"';
$str = preg_replace("/(\"[^\"]+\")\s/", "$1;", $str);

print_r($str);  // '"61926182767182";"DAS GEILE GAME";"HTTP://";"HTTP://";"32.59"'

(this approach can be easily extended if there would be tabs instead of spaces in the input string OR if there may be multiple spaces between words. It's more flexible)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download