Forum Moderators: coopster

Message Too Old, No Replies

bolding keyword is messing header

         

asmith20002

9:15 pm on Oct 4, 2008 (gmt 0)

10+ Year Member



Hey guys

I get all the page output and check it wit preg_replace to bold some words . but it will bold a word if it finds it in my page html header code and that's messing up my page.


$string = get_ob_content();
$words_to_bold = array("word1","word2","word3","word4");
echo preg_replace('~('.implode('¦', $words_to_bold).')~','<b>$0</b>',$string);}

How can i tell it to skip the content between the head tags?

Thanks

[edited by: eelixduppy at 9:47 pm (utc) on Oct. 4, 2008]
[edit reason] fixed formatting [/edit]

g1smd

9:17 pm on Oct 4, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Can you split out everything before <body>, work on the remainder and then join it back together?

eelixduppy

9:50 pm on Oct 4, 2008 (gmt 0)



Going with what g1smd said, strpos [php.net] will be useful here finding the location of
<body>
and
</body>
and grabbing the content in between. Once you find the location you just need the substr [php.net].

grallis

12:03 am on Oct 5, 2008 (gmt 0)

10+ Year Member



One way would be to find a <body tag before allowing the preg_replace to bold any words. Use a boolean datatype like $bodyFound = false; ... Once it's set to TRUE, allow the regular expression to bold the keywords.

Sorry g1smd, not trying to hijack your idea. Didnt fully read the answers before posting. My apologies :)

g1smd

12:10 am on Oct 5, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I was guessing, and not sure if someone else might come up with a better method... so don't apologise, thanks for posting.

asmith20002

6:49 am on Oct 5, 2008 (gmt 0)

10+ Year Member



Thanks guys the idea.

I haven't test it yet, but its obvious it will work. Have no idea why didn't came to my mind last night.

Thanks a lot
Cheers

asmith20002

7:46 am on Oct 5, 2008 (gmt 0)

10+ Year Member



What about skipping all html tags?

how to tell it "only bold" content which are not in the html tags?