Mark text in HTML

Posted by Alon on Stack Overflow See other posts from Stack Overflow or by Alon
Published on 2010-05-16T12:44:46Z Indexed on 2010/05/16 12:50 UTC
Read the original article Hit count: 590

Filed under:
|

Hi

I have some plain text and html. I need to create a PHP method that will return the same html, but with <span class="marked"> before any instances of the text and </span> after it.

Note, that it should support tags in the html (for example if the text is blabla so it should mark when it's bla<b>bla</b> or <a href="http://abc.com">bla</a>bla.

It should be incase sensitive and support long text (with multilines etc) either.

For example, if I call this function with the text "my name is josh" and the following html:

<html>
<head>
    <title>My Name Is Josh!!!</title>
</head>
<body>
    <h1>my name is <b>josh</b></h1>
    <div>
        <a href="http://www.names.com">my name</a> is josh
    </div>

    <u>my</u> <i>name</i> <b>is</b> <span style="font-family: Tahoma;">Josh</span>.
</body>
</html>

... it should return:

<html>
<head>
    <title><span class="marked">My Name Is Josh</span>!!!</title>
</head>
<body>
    <h1><span class="marked">my name is <b>josh</b></span></h1>
    <div>
        <span class="marked"><a href="http://www.names.com">my name</a> is josh</span>
    </div>

    <span class="marked"><u>my</u> <i>name</i> <b>is</b> <span style="font-family: Tahoma;">Josh</span></span>.
</body>
</html>

Thanks.

© Stack Overflow or respective owner

Related posts about html

Related posts about php