Jump to content

Skimming through a SQL post


Recommended Posts

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.

Link to comment
Share on other sites

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.

 

 

Link to comment
Share on other sites

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 = " "

Link to comment
Share on other sites

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>"

?>

Link to comment
Share on other sites

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.

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.