Question

I'm trying to work out how to replace a word in my website script that I run on a file import to clean up the data.

In my example I want to replace the Subcategory called "Seeds" to now be called "Plants & Seeds"

At the moment it looks like this:

[str_replace('Seeds','Plants & Seeds',{subcategory[1]})]

But that will also replace other Sub categories that also have the word "Seeds"

I have tried the following both without success( basically wild guessing):

[str_replace(':Seeds', '"Plants & Seeds"',{subcategory[1]})]

[str_replace($Seeds, "Plants & Seeds",{subcategory[1]})]

This must be on one line to work in my field with the import script on the website.

How could I do this?

Was it helpful?

Solution

$subcategory[1]="Seeds .. blablabla Seeds 2 Seeds 3 Seeds4";
echo preg_replace('/Seeds/', 'Plants & Seeds', $subcategory[1], 1); 
//Plants & Seeds .. blablabla Seeds 2 Seeds 3 Seeds4

have a nice day ;)

EDIT

$subcategory[1]="Seeds .. blablabla Seeds 2 Seeds 3 Seeds4, Pots Planters & Tools, Pots, Planters & Tools";
$seed_replaced=preg_replace('/Seeds/', 'Plants & Seeds', $subcategory[1], 1); 
$Pots_Planters_one=preg_replace('/Pots Planters & Tools/', 'Pots & Planters', $seed_replaced, 1); 
$all_replaced=preg_replace('/Pots, Planters & Tools/', 'Pots & Planters', $Pots_Planters_one, 1); 
echo $all_replaced;
//Plants & Seeds .. blablabla Seeds 2 Seeds 3 Seeds4, Pots & Planters, Pots & Planters
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top