Jump to content

[SOLVED] Clean up field in my SQL useing Regex to extract part of a url


JohnDeer

Recommended Posts

I have a column in a MYSQL database that  I need to try and clean up and I was told that maybe to use PHP would be the best bet.

 

Here is a copy of the field there are about 900 records like this in the database and I want to try and remove all of the HTML apart from the category name

 

<A href="http://www.mysite.com/blog/category/home-diy/"><IMG class="" height=149 src="http://www.mysite.com/blog/images/home.png" width=225></A> 

 

In this case the category name is home-diy every thing else can go :-) any help on this would be handy as I have never used php before.

Link to comment
Share on other sites

Hi effigy thank you fro the reply you are correct about the "home-diy" as the first category there are about 10 categories in total and they layout is the same here are a few more examples

 

category "shopping"

 

<A href="http://www.mysite.com/blog/category/shopping/"><IMG class="" height=149 src="http://www.mysite.com/blog/images/shop.png" width=225></A> 

 

category "pets"

 

<A href="http://www.mysite.com/blog/category/pets/"><IMG class="" height=149 src="http://www.mysite.com/blog/images/cat.png" width=225></A> 

 

category "money"

 

<A href="http://www.mysite.com/blog/category/money/"><IMG class="" height=149 src="http://www.mysite.com/blog/images/coins.png" width=225></A> 

Link to comment
Share on other sites

MYSQL 4.1

 

A friend said I should be able to do this direct in SQL

 

select SUBSTRING(oldCATEGORY,PATINDEX('%gory/%',oldCATEGORY)+5,(PATINDEX('%/">%',oldCATEGORY)) - (PATINDEX('%gory/%',oldCATEGORY)+5)) 

,oldCATEGORY

from database

where oldCategory <> ''

 

But looking at this it looks like it is for MSSQL

Link to comment
Share on other sites

True, but the same logic applies:

SELECT SUBSTRING(field, INSTR(field, 'gory/')+5, (INSTR(field, '/">')-(INSTR(field, 'gory/')+5))) AS category FROM table

I'm moving this to MySQL since it's not utilizing regular expressions.

Link to comment
Share on other sites

Thank you effigy :-) I was going to use a regular expression to clean it up in PHP as the SQL guys said it would be easy to do some thing like this in php but it looks like you proved them wrong and its just as easy to do it in SQL thak you for your time and help :-)

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.