Scholli Scholli - 1 month ago 8
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
CSV
. Its only an
TXT
file.
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()


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

Answer

The solution using preg_replace function:

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

print_r($str);  // '"61926182767182";"DAS GEILE GAME";"HTTP://google.com/123129182691239";"HTTP://google.com/123129182691239";"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)

Comments