Jump to content

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.