Delete everything after the line

I have a table with a field that contains descriptions for items sold on my site.

In our infinite wisdom, when we first launched the site, we entered the code for the Facebook Like button in the description, each of which is slightly different, because it contains product information in the link. Now we have about 400 elements with code.

This is an example of what I mean:

- Cooked, fluffy chocolate-chocolate flavor; Easy way to enjoy chocolate
- 45% less fat


<iframe src = "http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.oursite.com%2Findex.php%3Fmain_page%3Dproduct_info%26cPath%3D33_36%26products_id%3D106& layout = standard & show_faces = true & width = 450 & action = like & ColorScheme = light & height = 80 "scrolling =" no "frameborder =" 0 "style =" border: none; overflow: hidden; width: 450px; height : 80px; " allowTransparency = "true"> </iframe>

I want to remove the <iframe and the whole past. I looked through examples using REPLACE and / or LEFT, but cannot find what I need as I am not very good at SQL.

It will always be at the end of the products_description field, so I don’t have to worry about saving anything behind it, just before it.

The best I can come up with is

 SELECT LEFT(REPLACE('<iframe%','<iframe%',' '),0) 

but it does not work.

Thanks for any help you can offer regarding this, as it will take me much less time than editing each description.

UPDATE: still have not found the answer, having tried many of the options proposed. It starts, but does not edit the column.

I also tried this by finding it on another site. The same thing, but thought it might give someone an idea of ​​how to proceed:

 select left(`products_description`,instr('<iframe',`products_description`)-1) FROM products_description 
+4
source share
3 answers
 SELECT trim( -- trim removes spaces before and after given string left( 'some text < iframe', locate('< iframe', 'some text < iframe') - 1 ) ); 

For a better understanding, I did not remove the space between < and iframe .

You can also watch this topic:

Remove HTML tags from a post

but this discussion is only for removing tags and saving <tag> text </tag> between tags. In any case, this will be for you, because you have nothing between <iframe> and </iframe> .

+2
source
  • REPLACE('<iframe%','<iframe%',' ') will give you just '' - it searches for the first argument for the second argument and replaces it with the third.
  • LEFT(somestring, 0) will give you a string with 0 characters
+1
source

Try the following:

 select substr(your_raw_string, 0, instr(your_raw_string, '<iframe') -1) from your_table 
0
source

Source: https://habr.com/ru/post/1442904/


All Articles