Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Julian Ptak
Julian Ptak
30,920 Points

Stripping a string of both HTML and CSS using PHP?

Hey all,

I'm working on a project where the data I am receiving is heavily laden with inline css and I need just the text at its very heart inside a variable. I've been Googling for a while now and I've played with strip_tags() but this function seems to leave the inline CSS behind, which is a big no no for me. Does anyone know a way to remove not only the HTML tags but their inline CSS counterparts using PHP? Here is the code I'm trying to work with:

<?php echo strip_tags(do_shortcode('[cfdb-table form="Find Local Dealer" show="select-state" hide="company-name,contact-name,phone,email,address,city,zip-code,select-country,fax,findingUs,areasOfInterest,comments,captcha-872,Submitted Login,Submitted From,submit_time,$_POST(param),$_GET(param),$_COOKIE(param)" limit="0,1" header="false"]')); ?>

As you can see, it's a shortcode from Wordpress. But all this editing is being done in the template quickly after a form submission. So I just want to ditch the CSS and HTML so I can manipulate the text and use it for conditionals.

Somewhere online someone said something about using Regex for this but I'm not too handy with Regex at the moment and I wouldn't know where to start with it.

Any ideas?

Thanks!

2 Answers

Michael Collins
Michael Collins
433 Points

SimpleDomParser http://simplehtmldom.sourceforge.net/

This will do exactly what you are looking for.