Jump to content

preg_replace to add <p> tags to text not surrounded by specific charcters


giraffemedia

Recommended Posts

Hello,

 

I have two functions in my document that convert anything in a string between the characters [] as a link <a href="example.com">example</a> or {} as a paragraph <p class="example">example</p> like so…

 

Lorem ipsum dolor sit amet, consectetur adipiscing elit. 

Sed aliquet, ante et blandit fermentum, [www.example.com] sem elit fermentum odio, sit amet sagittis arcu libero vitae nunc. Sed sed viverra diam. Etiam tristique sagittis neque id aliquet. {Etiam quam enim, vestibulum nec hendrerit nec, faucibus ut est.} 

Donec vel orci eu elit pretium euismod ac vitae nisi. Praesent scelerisque aliquet nunc vel ultrices. Praesent interdum massa at dolor tempus aliquet. 

 

 

What I would like is a new function that wraps all other text in <p></p> tags but leaves the one's I have already processed. I want it to look like the following…

 

<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>

<p>Sed aliquet, ante et blandit fermentum, <a href="www.example.com">www.example.com</a> sem elit fermentum odio, sit amet sagittis arcu libero vitae nunc. Sed sed viverra diam. Etiam tristique sagittis neque id aliquet.</p>

<p class="example">Etiam quam enim, vestibulum nec hendrerit nec, faucibus ut est.</p>

<p>Donec vel orci eu elit pretium euismod ac vitae nisi. Praesent scelerisque aliquet nunc vel ultrices. Praesent interdum massa at dolor tempus aliquet.</p> 

 

Is it possible to write one function that does all of this in one?

 

Thanks,

 

James

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.