删除字符串后的所有内容 [英] Remove everything after string
问题描述
我有一个表格,其中的一个字段包含对我网站上出售的商品的描述.
I have a table with a field that contains descriptions for items sold on my site.
以我们无限的智慧,当我们第一次启动网站时,我们将 Facebook Like 按钮的代码粘贴到描述中,每个都略有不同,因为它在链接中有产品信息.我们现在有大约 400 个带有代码的项目.
In our infinite wisdom, when we first started the site, we pasted code for a Facebook Like button into the description, each just a little different, as it has product info in the link. We now have about 400 items with the code in it.
这是我正在处理的一个例子:
This is an example of what I'm dealing with:
-搅打起来,松软的巧克力味;享受巧克力的轻松方式
- 脂肪减少 45%
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&布局=标准&show_faces=true&width=450&action=like&colorscheme=light&height=80"scrolling="no" frameborder="0" style="border:none; overflow:hidden;宽度:450px;height:80px;" allowTransparency="true">
-Whipped Up, Fluffy Chocolate-On-Chocolate Taste; A Lighter 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>
我想删除 <iframe
以及它后面的所有内容.我一直在寻找使用 REPLACE 和/或 LEFT 的示例,但找不到我需要的内容,因为我不太精通 SQL 语言.
I want to remove the <iframe
and everything past it. I have been looking around for examples using REPLACE and/or LEFT, but can't find what I need, as I am not very well versed in SQL language.
它总是在 products_description 字段的末尾,所以我不需要担心保存任何超过它的东西,只保存在它之前.
It will always be at the end of the products_description field, so I don't need to worry about saving anything past it, only before it.
我能想到的最好的是
SELECT LEFT(REPLACE('<iframe%','<iframe%',' '),0)
但这似乎不起作用.
感谢您对此提供的任何帮助,因为与编辑每个描述相比,我花费的时间要少得多.
Thank you for any help you can offer on this, as it will take me much less time than editing each description.
更新:在尝试了许多建议的变体之后,仍然没有找到答案.它运行,但不对列进行编辑.
UPDATE: Still haven't found an answer, after trying many variations of what has been suggested. It runs, but makes no edit to the column.
我也试过这个,在另一个网站上找到后.相同,但认为这可能会让某人知道如何继续:
I also tried this, after finding it on another site. The same, but thought it might give someone an idea of how to proceed:
select left(`products_description`,instr('<iframe',`products_description`)-1) FROM products_description
推荐答案
SELECT
trim( -- trim removes spaces before and after given string
left(
'some text < iframe',
locate('< iframe', 'some text < iframe') - 1
)
);
为了更好地理解,我没有删除 <
和 iframe
之间的空格.
For better understanding i did not removed space between <
and iframe
.
你也可以看看这个帖子:
You may also take a look at this thread:
但这是关于仅删除标签并在标签之间保留
text 的讨论.无论如何它会适合你,因为你在
和
之间没有任何东西.
but this is discussion about removing tags only, and keeping <tag>
text</tag>
between tags. Anyway it will for for you, because you have nothing between <iframe>
and </iframe>
.
这篇关于删除字符串后的所有内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!