Preg replace ignore already replaced?

How can I replace words in string and ignore already replaced ones?

$about_me ="this is a how to make money make money money test";

$money_array1 = "how to make money
how to make cash
how to earn cash
how to earn money";

$money_array2 = "make money
make cash
earn cash
earn money";

$money_array3 = "money
cash";

$money_array1 = explode("\r\n", $money_array1);
$money_array2 = explode("\r\n", $money_array2);
$money_array3 = explode("\r\n", $money_array3);

I can’t see from the code exactly what it is you are trying to achieve, can you give a more detailed explanation perhaps with some examples?

In the past, if I’ve used a string-search to search for a word, and then replaced it using string manipulation, I simply do the next string-search starting from after the previous one.

You mean like this?

https://repl.it/repls/InfiniteActivePostgres

Thanks buddy for your replay. I mean that if you have “how to make money” in the string it will replace this and then “make money” and “money” so it will be a mess.

I need some how once you replace one word stop grabbing words from it to try to match other words.

$about_me ="this is a how to make money make money money test";

$money_array1 = "how to make money
how to make cash
how to earn cash
how to earn money";

$money_array2 = "make money
make cash
earn cash
earn money";

$money_array3 = "money
cash";

$money_array1 = explode("\r\n", $money_array1);
$money_array2 = explode("\r\n", $money_array2);
$money_array3 = explode("\r\n", $money_array3);

foreach($money_array1 as $vall)
{
$pattern = '/\b('.$vall.')\b/i';
$about_me = preg_replace($pattern, "<a href=\"/link_h\">".$vall."</a>", $about_me);	
}

foreach($money_array2 as $vall)
{
$pattern = '/\b('.$vall.')\b/i';
$about_me = preg_replace($pattern, "<a href=\"/link_h\">".$vall."</a>", $about_me);	
}

foreach($money_array3 as $vall)
{
$pattern = '/\b('.$vall.')\b/i';
$about_me = preg_replace($pattern, "<a href=\"/link_h\">".$vall."</a>", $about_me);	
}

I have no idea what you’re on about. Can you give an example of a supplied input and an expected output? No code, just a string that goes in, and what sting comes out.

Yes please. The best guess I have is the matches you want to replace “overlap”. eg.

“money” → “cash”
“make cash” → “earn cash”
“make money now” → “earn cash”

and that when you do replacing in a certain order it messes up the matching in the later replaces.

Thank yo all guys. One question please:

How can I match separate word (word not someword) with regex but ignore when:

#word
or
<a href"">word</a>

$regex = “/bwordb/

I this this work but can’t make it work with case?

/\b(?<!#)\w*earn money\b(?!.*?\<\/a\>)/i

this is a how to make money make money earn money #money test cash #cash making money earning money Make money Earn money

Correction: If there is a case sensitive word it wont match all words why?

I think you are looking for /( |^)word( |$)/i

Thanks guys I took all your replies into consideration and finally came up with a decent result.

Would you mind sharing your result with us for future reference?

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.