Jump to content

PHP Replace where needed ONLY


Valleriani

Recommended Posts

Right now I have this set of code

 

for ( $counter = 1; $counter <= $count; $counter += 1) {
$content['post_content'] = str_replace($pieces[$counter], '<strong>' . $pieces[$counter] . '</strong>', $content['post_content']);
}

 

What it does is change any keywords in the content to have a bold feel to it, pretty simple enough.

 

The issue is, sometimes the keyword is in image links or links in general, for example:

$content['post_content'] = There is a bull here, a big bull! <img src="www.bigbull.com">

 

Generally it should just bold the two first ones, but not the last one (One's in < >'s basically shouldn't be).. Right now it would come out like <img src="www.big<strong>bull</strong>.com"> Which is a nasty booboo...

 

Can anyone help me out, or give me advise on how to zap this issue? Thanks!

Link to comment
https://forums.phpfreaks.com/topic/51245-php-replace-where-needed-only/
Share on other sites

This is how I think you could do it

 

Keep that initial for loop.

 

Then do a search to find the location of the keyword. If it finds the location  Check the first character after and before the keyword, if before is a Space, after is a space, OR before is a period after is a space, OR before and after are spaces, then it isn't a part of a link like that.

 

Then you can add the strong.

 

Repeat that for all occurances of the keyword.

 

Something like that should work.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.