0

If someone posts a multi-line post that contained text and links, I want to be able to find and wrap the links with <p> tags, but I can only do it with one link at a time (source code comes from phpBB2 - clickable links function), which causes every link to be like this:

<p>http://www.bbc.co.uk/</p>
<p>http://www.bbc.co.uk/</p>
<p>http://www.bbc.co.uk/</p>

Where I want it to happen to be like this:

<p>http://www.bbc.co.uk/
http://www.bbc.co.uk/
http://www.bbc.co.uk/</p>

Cheers.

Artefacto
  • 96,375
  • 17
  • 202
  • 225
MacMac
  • 34,294
  • 55
  • 151
  • 222

1 Answers1

2

Feed it to DOM loadHTML function and getElementsByTagName('p'), make a reference with ->item(i) based on the ->length, get the nodeValue and just make a new paragraph with document.createElement, set the nodeValue to your string of nodeValues that you retrieved from the loop after concatenating them with \n<br> or something.

You shouldn't use regex for this.

meder omuraliev
  • 183,342
  • 71
  • 393
  • 434
  • Why? Why shouldn't I use regex for this? – MacMac Aug 17 '10 at 21:51
  • So many reasons. http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454 Essentially, HTML isn't a regular language, so regex doesn't have the power to fully and properly parse it. There are cases where regex can help you munge HTML, but it's rarely the right tool for the job. – Matt Luongo Mar 04 '11 at 22:58