dennismonsewicz Posted July 8, 2008 Share Posted July 8, 2008 Ok... how would i skim through a mysql post searching for a particular word or words? I am having a brain fart lol. Like I have a sql field that would contain HTML and I need for my php script to skim through the field and search for the word style or class and take out the word along with anything in quotes. Quote Link to comment Share on other sites More sharing options...
mbeals Posted July 8, 2008 Share Posted July 8, 2008 so you have a table with a single attribute containing the html? or is the html distributed through several attributes of the same row? If it is from one attribute: Select * from table where `HTML` like '%$tag%'; or Select * from table where `HTML` like REGEXP '$regex; %'s are wild, so option 1 will return all rows with $tag in the attribute HTML. The second option does the same thing as the first but searches the HTML attribute with the regular expression $regex. The other option is to create a full text index. If you are searching across several attributes, then full text is the way to go anyway. Google mysql fulltext and you will find a boatload of info on it. Quote Link to comment Share on other sites More sharing options...
dennismonsewicz Posted July 8, 2008 Author Share Posted July 8, 2008 I have a backend that you can create posts with, well it will write html for you in the text editor and when the information is dumped into the DB it keeps the html content. So there are <h4>, <p>, <span>, <br />, and many other tags that are inserted. Well I developed a tool with this backend that will allow for a person to select a post (that is approved and previously written) and will allow for a person to email the post to someone. I am looking to have PHP take the post out of the DB and strip out the class = "someclass" and style = " " Quote Link to comment Share on other sites More sharing options...
mbeals Posted July 8, 2008 Share Posted July 8, 2008 if you just want to get rid of the html tags, use strip_tags() it will strip out all tags. If you want to allow some, just pass a string of allowed tags as the second arg. ie <?php $string = "<h1>Hello <strong>World</strong></h1>"; echo strip_tags($string); // echos "Hello World"; echo strip_tags($string, '<strong>') // echos "Hello <strong>World</strong>" ?> Quote Link to comment Share on other sites More sharing options...
dennismonsewicz Posted July 8, 2008 Author Share Posted July 8, 2008 alright, I will have to try that. Is there a way to look for entities within the HTML tag (EX: <p class="header">) like i would want to take out the class="header" Quote Link to comment Share on other sites More sharing options...
mbeals Posted July 8, 2008 Share Posted July 8, 2008 look into preg_match_all(); it will let you run regular expression searches. you can get as crazy as you want. for example preg_match_all("/<(\w).*?>.*<\/\w>/",$text,$matches); will search the text and return an array of 2 arrays. The first will be everything enclosed in < > tags and the second corresponds to the 'type' of tag it is (p,ul,a, etc). depending on how you formulate the regex, you can pull out whatever you want. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.