www.webdeveloper.com
Results 1 to 4 of 4

Thread: preg_replace matching word with inner tag

  1. #1
    Join Date
    Aug 2004
    Posts
    244

    preg_replace matching word with inner tag

    Say you have a string:

    PHP Code:
    $str='This is a<wbr /> line of te<wbr />xt'
    I want to wrap certain words with a span tag, like:

    PHP Code:
    preg_replace('/text/','<span>$1</span>',$str); 
    But obviously that doesnt work because in the string there is a '<wbr />' in the middle of it. Note that I can't strip out the '<wbr />'.

    Does anyone know how I would go about doing this?

    Many thanks!

  2. #2
    Join Date
    Aug 2004
    Posts
    244
    By the way, it's worth pointing out that there won't necessarily be a '<wbr />' in the middle of the word and the location of it is arbitrary.. e.g.

    PHP Code:
    $str='This is a<wbr /> line of te<wbr />xt, look at this text and how good this tex<wbr />t is.'
    Also there could be more than one, such as:

    PHP Code:
    $str='t<wbr />e<wbr />xt'
    Last edited by artemis; 08-23-2007 at 09:20 AM.

  3. #3
    Join Date
    Aug 2004
    Posts
    244
    Nevermind, after a phat amount of regexing i figured it out

  4. #4
    Join Date
    Feb 2011
    Posts
    1
    hi i would be very interested in your olution if you still remember it.

    I have this as an input:

    <p>Hello wo<em>rld</em></p>
    <p>How are you <span style='text-decoration:underline'>doing?</span></p>

    i need to have the following output:

    <p><span id='w_1'>Hello</span> <span id='w_2'>wo<em>rld</em></span></p>
    <p><span id='w_3'>How</span> <span id='w_4'>are</span> <span id='w_5'>you</span> <span id='w_6'><span style='text-decoration:underline'>doing?</span></span></p>


    thanks in advance !

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles