The regular expression problem I am having is driving me absolutely nuts! I have spent hours on this to no avail. If anyone has any input it would be much appreciated.
Here's some text...
Code: Select all
About Me.
My profesional goal is to develop a client base that will allow me to make a living entirely
I am well on the way to realizing that goal and already work from home but am looking
The kind of web development services that I offer are not just related to what one
To see a more detailed listing and explanation of the kinds of things that I can do
Carlos
I want to insert the HTML <p></p> tags around each paragraph. No problem using the following regular expression function (passing the text above to it in the $text parameter)...
Code: Select all
function addParagraphs($text)
{
// Add paragraph elements
$lf = chr(10);
return preg_replace('/
\n
(.*)
\n
/Ux' , $lf.'<p>'.$lf.'$1'.$lf.'</p>'.$lf, $text);
}
The output from running the text through the above function is...
Code: Select all
<p>
About Me.
</p>
<p>
My profesional goal is to develop a client base that will allow me to make a living entirely
</p>
<p>
I am well on the way to realizing that goal and already work from home but am looking
</p>
<p>
The kind of web development services that I offer are not just related to what one
</p>
<p>
To see a more detailed listing and explanation of the kinds of things that I can do
</p>
<p>
Carlos
</p>
But I am trying to get the paragraph function to ignore paragraphs that are already hand code with any kind of HTML tag around it. So for example if the first paragraph in my text file was...
Code: Select all
<p class="someclass">
About Me
</p>
I would want the function to put the <p></p> tags around every other paragraph but the first one. I want the function to leave any paragraphs that already have HTML tags around them alone.
Unfortunately having a first paragraph like the one above in the text breaks the function and messes things up.
What happens is the function matches for the newline after "About Me" and then puts the paragraph tags around the following "</p>" resulting in...
Code: Select all
<p class="someclass">
About Me
<p>
</p>
</p>
Anybody got any suggestions?
Thanks.
Carlos